Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Run results for:
Map vs Map vs Foreach vs for
Is it faster to store the return value of the map function in a new variable or to overwrite the existing array? Or is it faster to use forEach?
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser:
Chrome 131
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
one year ago
Benchmark engine:
Benchmark.js
forEach
1.4M/s
for
439K/s
Dont overwrite
366K/s
Overwrite
329K/s
View exact numbers
Test name
Executions per second
✓
forEach
1,405,743 Ops/sec
for
438,652 Ops/sec
Dont overwrite
365,639 Ops/sec
Overwrite
328,989 Ops/sec
Script Preparation code:
var a = new Array(1000); for (let i = 0; i < a.length; i++) { a[i] = {}; }
Tests:
Overwrite
a = a.map((row) => { row.checked = true; return row; });
Dont overwrite
const b = a.map((row) => { row.checked = true; return row; });
forEach
a.forEach((row) => { row.checked = true; });
for
for(let i=0; i<a.length; i++) { a[i].checked = true; }