Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Array loop vs foreach vs map vs for of
(version: 0)
Benchmark.js
Comparing performance of:
foreach vs for vs map vs for of
Created:
6 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var arr = []; for (var i = 0; i < 100000; i++) { arr[i] = i; } function someFn(i) { return i * 3 * 8; }
Tests:
foreach
const res = [] arr.forEach(function (item){ res.push(someFn(item)) })
for
const res = [] for (var i = 0, len = arr.length; i < len; i++) { res.push(someFn(arr[i])) }
map
const res = arr.map(item => someFn(item))
for of
const res = [] for (const el of arr) { res.push(someFn(el)) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
foreach
for
map
for of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser/OS:
Chrome 140 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
foreach
1K/s
map
1K/s
for of
986/s
for
900/s
View exact numbers
Test name
Executions per second
✓
foreach
1,491 Ops/sec
map
1,279 Ops/sec
for of
986 Ops/sec
for
900 Ops/sec
Related benchmarks
Array loop vs for of loop vs foreach vs map (2)
Array loop: forEach vs for vs map vs for of entries
Array loop vs foreach vs map with large array
Comments
Confirm delete:
Do you really want to delete benchmark?