Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
for vs for...of vs .map vs .forEach
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0
Browser:
Chrome 144
Operating system:
Windows
Device Platform:
Desktop
Date tested:
6 months ago
for with init array
403/s
for with init length and array
392/s
for ... of
185/s
.map
101/s
.forEach
96/s
View exact numbers
Test name
Executions per second
✓
for with init array
403 Ops/sec
for with init length and array
392 Ops/sec
for ... of
185 Ops/sec
.map
101 Ops/sec
.forEach
96 Ops/sec
Script Preparation code:
function generateTestArray() { const result = []; for (let i = 0; i < 1000000; ++i) { result.push(i); } return result; }
Tests:
for with init array
const array = generateTestArray(); const output = new Array(array.length); for (let i = 0; i < array.length; ++i) { output[i] = array[i]; }
for with init length and array
const array = generateTestArray(); const length = array.length; const output = new Array(length); for (let i = 0; i < length; ++i) { output[i] = array[i]; }
for ... of
const array = generateTestArray(); const output = []; for (const x of array) { output.push(x); }
.map
const array = generateTestArray(); const output = array.map(x => x);
.forEach
const array = generateTestArray(); const output = []; array.forEach(x => output.push(x));