资讯专栏INFORMATION COLUMN

JavaScript数组去重最佳选择

reclay / 2399人阅读

摘要:时间为如果依赖排序在字符和数字组合中更是依赖大大增加的时间,并且影响了原有数组顺序。时间为,同为循环对比,但是增加了内层循环次数。总体来说最佳选择为但是推荐使用大道至简

数组值只包含了字符和数字,更多类型增加不会影响以下method_*的排序(时间排序)

测试环境:版本 57.0.2987.133 (64-bit)

var arr1 = [1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 1, 2, 3, 4, 5, 6, "1", "2", "1", "2"];
var arr2 = [1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 1, 2, 3, 4, 5, 6];

while(arr1.length < 600000){

    arr1 = arr1.concat(arr1);
    arr2 = arr2.concat(arr2);
}
// 
// 
// 
// method_1:新建数组-双循环-对比-push
Array.prototype.unique = function(){

    var res = [this[0]],
        len = this.length;

    for(var i = 1; i < len; i ++){

        var repeat = false,
            rlen     = res.length;

        for (var j = 0; j < rlen; j ++){

            if(this[i] === res[j]){

                repeat = true;
                break;
            }
        }

        !repeat && res.push(this[i]);
    }

    return res;
}

var timestamp = new Date().getTime();
arr1 = arr1.unique();
console.log(new Date().getTime() - timestamp); // 平均 18

var timestamp = new Date().getTime();
arr2 = arr2.unique();
console.log(new Date().getTime() - timestamp); // 平均 10
// 
// 
// 
// method_2:新建数组-排序-单循环-对比-push
Array.prototype.unique = function () {
 
    this.sort(function (a, b) {
     
        if (typeof a != typeof b && a == b) {
     
            if (typeof a == "number") {
                return -1;
            } else {
                return 1
            }
        }
        return parseInt(a) - parseInt(b);
    })

    var res = [this[0]],
        len = this.length;

    for (var i = 1; i < len; i ++) {

        var slen = res.length;


        if (this[i] !== res[slen -1]) {

            res.push(this[i]);
        }
    }

    return res;
}

var timestamp = new Date().getTime();
arr1 = arr1.unique();
console.log(new Date().getTime() - timestamp); // 平均 121

var timestamp = new Date().getTime();
arr2 = arr2.unique();
console.log(new Date().getTime() - timestamp); // 平均 93
// 
// 
// 
// method_3:新建数组-新建对象-单循环-对象对比-数组push 
Array.prototype.unique = function () {

    var res = [],
        obj = {},
        len = this.length;

    for (var i = 0; i < len; i++) {

        // 由于对象属性为字符串 1 和 "1" 相同,因此做不到 ===
        !obj[this[i]] && res.push(this[i]) && (obj[this[i]] = 1);

    }
    return res;
}
var timestamp = new Date().getTime();
arr1 = arr1.unique();
console.log(new Date().getTime() - timestamp); // 平均 8

var timestamp = new Date().getTime();
arr2 = arr2.unique();
console.log(new Date().getTime() - timestamp); // 平均 5
// 
// 
// method_4:新建数组-循环-向后查询-数组push
Array.prototype.unique = function () {
 
    var res = [],
        len = this.length,
        i, j;
    for (i = 0; i < len; i++) {

        for (j = i + 1; j < len; j++) {

            if (this[i] === this[j]) {

                j = false;
                break;
            }
        }
        
        j && res.push(this[i]);
    }
    
    return res;
}
var timestamp = new Date().getTime();
arr1 = arr1.unique();
console.log(new Date().getTime() - timestamp); // 平均 28

var timestamp = new Date().getTime();
arr2 = arr2.unique();
console.log(new Date().getTime() - timestamp); // 平均 17
// 
// 
// 
// method_4:使用Set
Array.prototype.unique = function (){

    var arr = new Set(this);

    return [...arr];
}
var timestamp = new Date().getTime();
arr1 = arr1.unique();
console.log(new Date().getTime() - timestamp); // 平均 75

var timestamp = new Date().getTime();
arr2 = arr2.unique();
console.log(new Date().getTime() - timestamp); // 平均 60
// 
// 
// 
// method_5:使用find/findIndex/indexOf
Array.prototype.unique = function(){

    var res = [this[0]],
        len = this.length;

    for(var i = 1; i < len; i ++){

        var repeat = false,
            rlen     = res.length;

        if(!res.find((v,k)=>{return v === this[i]})){
            
            res.push(this[i]);
        }

        // if(res.indexOf(this[i]) == -1){

        //     res.push(this[i]);   
        // }

        // if(res.findIndex((v,k)=>{return v === this[i]}) == -1){
            
        //     res.push(this[i]);
        // }
    }

    return res;
}
var timestamp = new Date().getTime();
arr1 = arr1.unique();
console.log(new Date().getTime() - timestamp); // 平均 110+

var timestamp = new Date().getTime();
arr2 = arr2.unique();
console.log(new Date().getTime() - timestamp); // 平均 65+
总结

method_1时间为 18 /10 ,循环对比。

method_2时间为 121/93 , 如果依赖sort排序(在字符和数字组合中更是依赖sort callback)大大增加的时间,并且影响了原有数组顺序。

method_3时间为 8/5 , 无法区分字符和数字。

method_4时间为 28/7 ,同method_1为循环对比,但是增加了内层循环次数。

method_5时间为 75/60 , 使用Set数据结构特性。

method_5时间为 110+/65+ , 使用find/findIndex/indexOf判断。

总体来说最佳选择为 method_1

但是推荐使用method_5----大道至简

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/82709.html

相关文章

  • 记一次JavaScript API练习题

    摘要:当我完成这个题目并且看到其他大神的答案时,我就觉得我真的很有必要记录一下这道题,并且思考它在中的实现。表示被查找的值方法返回一个由替换值替换一些或所有匹配的模式后的新字符串。举一反三,多多思考,多多实践才是学习前端的最佳实践。 之前,我在Codewars上看到一道名为Recover a secret string from random triplets的题,这道题使我沉思了很久,最终...

    scq000 评论0 收藏0
  • js基本操作-数组去重

    摘要:基本操作数组去重写在前面数组去重经常出现在前端招聘的笔试题里,比如有数组,请用实现去重函数,使得返回作为笔试题,考点有二正确。基本介绍文章主要是对数组去重的常用方法进行介绍。 js基本操作-数组去重 写在前面 JavaScript 数组去重经常出现在前端招聘的笔试题里,比如: 有数组 var arr = [a, b, c, 1, 0, c, 1, , 1, 0],请用 JavaScr...

    blastz 评论0 收藏0
  • JavaScript专题之数组去重

    摘要:专题系列第三篇,讲解各种数组去重方法,并且跟着写一个前言数组去重方法老生常谈,既然是常谈,我也来谈谈。它类似于数组,但是成员的值都是唯一的,没有重复的值。 JavaScript 专题系列第三篇,讲解各种数组去重方法,并且跟着 underscore 写一个 unique API 前言 数组去重方法老生常谈,既然是常谈,我也来谈谈。 双层循环 也许我们首先想到的是使用 indexOf 来循...

    fsmStudy 评论0 收藏0
  • 温故js系列(7)-数组去重由慢到快由繁到简

    摘要:前端学习教程开发模块化规范化工程化优化工具调试值得关注的博客面试前端资源汇总欢迎提斧正数组去重数组去重由慢到快由繁到简演化去重写法,箭头函数为新写法。在去重过程中,原数组都是不变的。它类似于数组,但是成员的值都是唯一的,没有重复的值。 前端学习:教程&开发模块化/规范化/工程化/优化&工具/调试&值得关注的博客/Git&面试-前端资源汇总 欢迎提issues斧正:数组去重 JavaSc...

    mgckid 评论0 收藏0
  • 前端常用代码片段(四)

    摘要:尽量避免使用表达式又称动态属性。使用计算数组中的重复项如果你想计算数组中的每个值有多少重复值,也可以快速帮到你。 前端常用代码片段(一) 点这里前端常用代码片段(二) 点这里前端常用代码片段(三) 点这里前端常用代码片段(四) 点这里前端常用代码片段(五) 点这里前端常用代码片段(六) 点这里 1.简述一下你对HTML语义化的理解?并写出一段语义化的HTML? 语义化是指根据内容的结...

    worldligang 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<