如果我们在循环中访问当前项目,那么/ forEach的性能更高?

时间:2017-12-11 16:20:56

标签: javascript node.js performance for-loop foreach

每个人说for的地方都比forEach更高效  例如

https://jsperf.com/for-vs-foreach/37

Javascript efficiency: 'for' vs 'forEach'

  

但是在循环中访问当前元素时它是否成立。

     

如果不是,为什么不,如果是,那么下面的代码呢,这在不同情况下给出了相互矛盾的结果:

逻辑:

//----------------------------------------
var arr = [], len = 1000000, nos = 100;

for(var t = 0; t<len; t++) {
  arr.push({
    a:'a',b:'b',t:Math.random()
  });
}
//----------------------------------------

//===========forEach start====================
var tp1 =  new Date();
arr.forEach( (ik)=>{
  let tmp = [];
  for(let x = 0;x<nos;x++){
    tmp.push(ik.a);
  }
});
var tp2 =  new Date();
//===========forEach end====================


//===========for loop start(let outside loop)====================
var t1 =  new Date();
let i;
for(i = 0; i<arr.length; i++){
  let tmp = [];
  for(let x = 0;x<nos;x++){
    tmp.push(arr[i].a);
  }
}
var t2 =  new Date();
//===========for loop end(let outside loop)====================


//===========for loop start(let inside loop)====================
var ti1 =  new Date();
for(let i = 0; i<arr.length; i++){
  let tmp = [];
  for(let x = 0;x<nos;x++){
    tmp.push(arr[i].a);
  }
}
var ti2 =  new Date();
//===========for loop start(let inside loop)====================


//----------------------------------------

var dff = t2-t1;
var dffi = ti2-ti1;
var dffp = tp2-tp1;

console.log("Execution Time:- (let outside loop): ",dff);
console.log("Execution Time:- (let inside loop): ",dffi);
console.log("Execution Time:- forEach: ",dffp);

console.log("Execution Time:- forEach - for(inside loop): ",dffp-dffi);
console.log("Execution Time:- forEach - for(outside loop): ",dffp-dff);

//----------------------------------------

其中输出如下:

案例1:nos = 100

Execution Time:- (let outside loop):  3739
Execution Time:- (let inside loop):  3699
Execution Time:- forEach:  730
Execution Time:- forEach - for(inside loop):  -2969
Execution Time:- forEach - for(outside loop):  -3009

案例2:nos = 10

Execution Time:- (let outside loop):  427
Execution Time:- (let inside loop):  396
Execution Time:- forEach:  98
Execution Time:- forEach - for(inside loop):  -298
Execution Time:- forEach - for(outside loop):  -329

案例3:nos = 2

Execution Time:- (let outside loop):  106
Execution Time:- (let inside loop):  125
Execution Time:- forEach:  90
Execution Time:- forEach - for(inside loop):  -35
Execution Time:- forEach - for(outside loop):  -16

案例4:nos = 1

Execution Time:- (let outside loop):  62
Execution Time:- (let inside loop):  85
Execution Time:- forEach:  72
Execution Time:- forEach - for(inside loop):  -13
Execution Time:- forEach - for(outside loop):  10

案例5:nos = 0

Execution Time:- (let outside loop):  17
Execution Time:- (let inside loop):  23
Execution Time:- forEach:  72
Execution Time:- forEach - for(inside loop):  49
Execution Time:- forEach - for(outside loop):  55

1 个答案:

答案 0 :(得分:1)

经过一些分析后,我减少了以下一些事实:

  • 基本上for循环比执行
  • 更有效
  • 两者都有一个用于循环的本机(c / c ++)实现。
  • forEach具有本机(在c / c ++层)实现,用于访问回调中提供的当前元素。

推导

  1. 如果我们在迭代时访问当前元素的第三点,forEach将提供更快的结果,因为它将在本机级别执行但是在for循环的情况下它必须传递到两个层{ {1}}然后JS

  2. 对于current_item,index,array,native有更高效的变量解析,因为它们是通过本机方法的回调传递的,但是forEach不是