Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array loop vs foreach vs map vs for of
(version: 0)
Comparing performance of:
foreach vs for vs map vs for of
Created:
5 years ago
by:
Guest
Jump 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:
7 months 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
Test name
Executions per second
foreach
1490.7 Ops/sec
for
899.7 Ops/sec
map
1279.2 Ops/sec
for of
985.9 Ops/sec
Related benchmarks:
Array loop vs for of loop vs foreach vs map (2)
Array loop vs foreach vs map (Small arrays)
Array loop: forEach vs for vs map vs for of entries
Array loop vs foreach vs map with large array
Array loop vs for of loop vs foreach vs map fixed
Comments
Confirm delete:
Do you really want to delete benchmark?