Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Performance of JavaScript .forEach() vs for()
(version: 1)
Comparing performance of:
.forEach vs .for (init array)
Created:
10 months ago
by:
Guest
Go 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; }
Tests:
.forEach
const array = generateTestArray(); array.forEach((x) => { x.r = x.a + x.b; });
.for (init array)
const array = generateTestArray(); const r = new Array(array.length); for (let i = 0; i < array.length; ++i) { r[i] = array[i].a + array[i].b; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
.forEach
.for (init array)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0
Browser/OS:
Firefox 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
.for (init array)
20/s
.forEach
19/s
View exact numbers
Test name
Executions per second
✓
.for (init array)
20 Ops/sec
.forEach
19 Ops/sec
Related benchmarks
Performance of JavaScript .forEach, .map and .reduce vs for and for..of
Performance of JavaScript .forEach, .map and .reduce vs for and for..of with 1000p
Performance of JavaScript .forEach vs for..of
Performance of JavaScript .forEach, .map and .reduce vs for and for..of 2232132
Comments
Confirm delete:
Do you really want to delete benchmark?