forEach vs for(i => n) (v2)

Revision 2 of this benchmark created on


Description

Testing which is more performant on large objects..

Preparation HTML

<div id="dataContainer" style="display: none;" />

<script>
const data = Array.from({ length: 50000 }, (v, i) => ({id: i + 1, num: i}));
// Convert data to JSON and store it in the div    

document.getElementById('dataContainer').textContent = JSON.stringify(data);
</script>

Setup

// Extract the data from the div
const rawData = document.getElementById('dataContainer').textContent;
const data = JSON.parse(rawData);
let total = 0;
const summ = (_) => total += _.num;

Teardown

total = 0;

Test runner

Ready to run.

Testing in
TestOps/sec
anon forEach loop
data.forEach((_) => total += _.num)
ready
raw for loop
for (let counter = 0; counter < data.length; counter++) {
  total += data[counter].num;
}
ready
non anon forEach loop
data.forEach(summ)
ready

Revisions

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