native forEach vs lodash (v6)

Revision 6 of this benchmark created on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.js"></script>

Setup

let length = 10000;
let array = new Array(length);
    
for (var i = 0; i < length; i++) {
  array[i] = i;
}

let getAddTo = () => {
  let sum = 0;
  return (d) => { sum += d; }
}

Test runner

Ready to run.

Testing in
TestOps/sec
native
// we're basically doing a reduce, but testing forEach specifically
array.forEach(getAddTo());
ready
lodash
// we're basically doing a reduce, but testing forEach specifically
_.forEach(array, getAddTo());
ready
for loop
let addTo = getAddTo();

for(let i=0 ; i < length; i++){ 
  addTo(array[i]); 
}
ready
while loop
let addTo = getAddTo();
let i = 0;

while(++i < length){
  addTo(array[i]);
}
ready
naive forEach implementation
_.forEach(array, getAddTo());
ready
for in
let addTo = getAddTo();

for(let i in array){ addTo(i); }
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.