Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
for vs foreach vs for..of with sum
Compare loop performance
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:135.0) Gecko/20100101 Firefox/135.0
Browser:
Firefox 135
Operating system:
Linux
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
for
166.3 Ops/sec
foreach
87.9 Ops/sec
for..of with reassign
91.5 Ops/sec
for..of with no reassign
39.7 Ops/sec
Script Preparation code:
function generateTestArray() { const result = []; for (let i = 0; i < 1000000; ++i) { result.push({ a: i, b: i / 2, r: 0, }); } return result; } const array = generateTestArray();
Tests:
for
for (let i = 0; i < array.length; i++) { array[i].r = array[i].a + array[i].b; }
foreach
sum = x => x.r = x.a + x.b; array.forEach(sum);
for..of with reassign
for (let x of array) { x.r = x.a + x.b; }
for..of with no reassign
const result = []; for (let x of array) { result.push(x.a + x.b); }