Posted by Arjun Arjun on Friday 27th April 2018, 1 minute read

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

...
Arjun

I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.