首页 > js数组去重方法如何进行性能对比

js数组去重方法如何进行性能对比

收集了几种数组去重方法,但是在对比性能的时候,原本采取对同一数组去重100000次,后来想到会不会浏览器对同一数组的去重会缓存起来,能说明这种性能对比方法的正确性么,或者提供比较好的方法给我


let arr1 = [1, 1, 1, 2, 3, 2, 4];
let es6 = function() {
    for(let i = 100000; i > 0; i--){
        let array = Array.from(new Set(arr1));
    }
};
console.time('es6');
console.timeEnd('es6');
let es5 = arr => { 
    for(let i = 100000; i > 0; i--){
        arr1.filter(function (value, index, array) { return index <= array.indexOf(value);}); 
    }
};
console.time('es5');
console.timeEnd('es5');

使用Benchmark.js和jsPerf分析代码性能
这篇文章介绍了工具Benchmark.jsjsPerf,不过后者挂了好久。

【热门文章】
【热门文章】