Which is more performant for/forEach if we are accessing current item in loop?

问题内容:

Every where people are saying for is more performant then forEach
e.g.

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

Javascript efficiency: ‘for’ vs ‘forEach’

But does it holds true when accessing current element in loop.

If not why not if yes what about following code, which gives contradictory results in different cases:

Logic:

//----------------------------------------
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);

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

//----------------------------------------
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);

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

Which gives output as follows:

case 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

case 2: 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

case 3: 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

case 4: 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

问题评论:

原文地址:

https://stackoverflow.com/questions/47757080/which-is-more-performant-for-foreach-if-we-are-accessing-current-item-in-loop

添加评论

友情链接:蝴蝶教程