• js_数组去重效率对比


    学习javascript已经快两年了,也不知道到了什么程度了。

    说说我对javascript的理解,在电脑的世界里,只有数据。

    数组,对象,字符串。对这些数据进行操作就可以完成很多业务逻辑,和页面的交互。

    对这些数据进行交互会有很多方法,有些效率高(耗时低),有些效率低(耗时高),这就确定你的代码运行的快慢了。

    下面是一个测试数组去重的方法。


     1 <!DOCTYPE html>
     2 <html>
     3 
     4     <head>
     5         <meta charset="UTF-8">
     6         <title></title>
     7     </head>
     8 
     9     <body>
    10 
    11         <script>
    12             //检查耗时逻辑
    13             let arr1 = Array.from(new Array(100000), (x, index) => {
    14                 return index
    15             })
    16 
    17             let arr2 = Array.from(new Array(50000), (x, index) => {
    18                 return index + index
    19             })
    20 
    21             let start = new Date().getTime()
    22             console.log('开始数组去重')
    23             console.log('去重后的长度', distinct(arr1, arr2).length)
    24 
    25             let end = new Date().getTime()
    26             console.log('耗时', end - start)
    27             //结束
    28 
    29             //方法一,耗时5108
    30             /*function distinct(a, b) {
    31                 let arr = a.concat(b);
    32                 return arr.filter((item, index) => {
    33                     return arr.indexOf(item) === index
    34                 })
    35             }*/
    36 
    37             //方法二
    38             /*function distinct(a, b) {
    39                 let arr = a.concat(b);
    40                 for(let i = 0, len = arr.length; i < len; i++) {
    41                     for(let j = i + 1; j < len; j++) {
    42                         if(arr[i] == arr[j]) {
    43                             arr.splice(j, 1);
    44                             // splice 会改变数组长度,所以要将数组长度 len 和下标 j 减一
    45                             len--;
    46                             j--;
    47                         }
    48                     }
    49                 }
    50                 return arr
    51             }*/
    52 
    53             //方法三,耗时152
    54             /*function distinct(a, b) {
    55                 return Array.from(new Set([...a, ...b]))
    56             }*/
    57 
    58             //方法四,耗时39
    59             function distinct(a, b) {
    60                 let arr = a.concat(b)
    61                 let result = []
    62                 let obj = {}
    63 
    64                 for(let i of arr) {
    65                     if(!obj[i]) {
    66                         result.push(i)
    67                         obj[i] = 1
    68                     }
    69                 }
    70                 return result
    71             }
    72         </script>
    73     </body>
    74 
    75 </html>
    View Code

     

    原文链接

  • 相关阅读:
    ubuntu下配置Apache
    ubuntu 下配置Web服务器
    ubuntu 笔记一
    域名解析
    C# Enum,Int,String的互相转换
    C# 得到本机局域网IP地址
    C# 连接 SQLServer 及操作
    C# OpenFileDialog 使用
    如何解决 IntelliJ Idea 编译 Java 项目时,找不到包或找不到符号的问题?
    阿里巴巴 MySQL 数据库之 SQL 语句规约 (三)
  • 原文地址:https://www.cnblogs.com/wush-1215/p/9753076.html
Copyright © 2020-2023  润新知