Skip to content

Use for() instead Array.forEach(), for() is 95% faster

Array.ForEach is about 95% slower than for() in for each for Arrays in JavaScript.

So, don’t use:

var data = [];
arr.forEach(function (item) {
  data.push(item);
})

Use:

var data = [];
for (var i = 0, len = arr.length; i < len; i++) {
  data.push(arr[i]);
}

See this performance test online: http://jsperf.com/fast-array-foreach

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments