Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for..of with sum
(version: 1)
Compare loop performance
Comparing performance of:
for vs foreach vs for..of with reassign vs for..of with no reassign
Created:
one year ago
by:
Guest
Jump to the latest result
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); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for
foreach
for..of with reassign
for..of with no reassign
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
165.1 Ops/sec
foreach
60.7 Ops/sec
for..of with reassign
90.2 Ops/sec
for..of with no reassign
41.9 Ops/sec
Related benchmarks:
Performance of JavaScript .forEach, .map and .reduce vs for and for..of
js array perfomance
Performance of JavaScript .forEach, .map and .reduce vs for and for..ofCXCcccc
for vs foreach ej
Performance of JavaScript .forEach, .map and .reduce vs for and for..of2
for-of-foreach
Performance of JavaScript .forEach, .map and .reduce vs for and for..of with 1000p
Performance of JavaScript .forEach vs for..of
Performance of JS .foreach, for, for...of
Comments
Confirm delete:
Do you really want to delete benchmark?