1

数组

    var arr = new Array(10000000);
    var arr1 = [];
    var arr2 = [];
    var arr3 = [];
    var arr4 = [];
    var arr5 = [];
    var arr6 = [];
 
    arr.fill(1);
 
    console.time('for优化版');
    for(var i = 0,len = arr.length;i < len;i ++){
        arr1.push(arr[i]);
    }
    console.timeEnd('for优化版');
 
    //for
    console.time('for');
    for(var i = 0;i < arr.length;i ++){
        arr2.push(arr[i]);
    }
    console.timeEnd('for');
 
    //forEach
    console.time('forEach');
    arr.forEach(function(val){
        arr3.push(val);
    });
    console.timeEnd('forEach');
 
    //for in
    console.time('for in');
    for(var b in arr){
        arr4.push(arr[b]);
    }
    console.timeEnd('for in');
 
    //map
    console.time('map');
    arr.map(function(val){
        arr5.push(val);
    });
    console.timeEnd('map');
 
    //for of
    console.time('for of');
    for(var d of arr){
        arr6.push(arr[d]);
    }
    console.timeEnd('for of');

谷歌 版本 70.0.3538.110(正式版本) (64 位):
for优化版: 221.882080078125ms
for     : 1106.947998046875ms
forEach : 366.0771484375ms
forin   : 3758.628173828125ms
map     : 2020.76416015625ms
forof   : 1721.920166015625ms
 火狐 版本 63.03 :
for优化版: 9959ms
for      : 10192ms 
forEach  : 1286ms
forin    : 9261ms
map      : 1776ms
forof    : 6659ms

结论

   这是一个尴尬的结论,谷歌vs火狐,同样是最新版本 或许谷歌才是王道,火狐跳舞吧,颤抖吧,你也太慢了吧, 以谷歌为准相对来说forof作为新的ES6遍历方法具有可观的效率

对象

        var obj = {};
    for(var i = 0,j = 10000000;i < j;i ++){
        obj[i] = i;
    }
    console.log(obj);
    var obj1 = {};
    var obj2 = {};
    var obj3 = {};
    var obj4 = {};
    var obj5 = {};
    var obj6 = {};
    console.time('foreach');
    Object.keys(obj).forEach(function(key){
//             obj1[key] = obj[key];
    });
    console.timeEnd('foreach');
    console.time('forin');
    for(var key in obj) {
//             obj2[key] = obj[key];
    }
    console.timeEnd('forin');
    console.time('getOwnPropertyNames');
    Object.getOwnPropertyNames(obj).forEach(function(key){
//             obj3[key] = obj[key];
    });
    console.timeEnd('getOwnPropertyNames');
    console.time('Reflect');
    Reflect.ownKeys(obj).forEach(function(key){
//             obj4[key] = obj[key];
    });        
    console.timeEnd('Reflect');
    console.log('---------------------------------------------------')
    console.time('forofentry');
    for (let [key, value] of Object.entries(obj)) {
//             obj5[key] =  value;
    }
    console.timeEnd('forofentry');
    console.time('forofkey');
    for (let key of Object.keys(obj)) {
//             obj6[key] =  obj[key];
    }
    console.timeEnd('forofkey');
--------------------- 
谷歌 版本 70.0.3538.110(正式版本) (64 位):
foreach            : 3097.0302734375ms
forin              : 2746.07666015625ms
getOwnPropertyNames: 7264.22412109375ms
Reflect            : 7151.320068359375ms
       ----------------------------------
forofentry         : 16713.181884765625ms
forofkey           : 5213.873046875ms
火狐 版本 63.03 :
foreach            : 727ms
forin              : 3409ms 
getOwnPropertyNames: 775ms
Reflect            : 749ms
       ----------------------------------
forofentry         : 6950ms
forofkey           : 1274ms

结论

   这是一个尴尬的结论,谷歌vs火狐,同样是最新版本 有点尴尬, 以谷歌为准相对来说forin具有可观的效率

结论

   遍历数组有优化版的for循环,遍历对象用forin

镰月
313 声望13 粉丝

它的优秀之处并非原创,它的原创之处并不优秀。