Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array loop vs foreach vs map vs while (fixed v2)
(version: 2)
Comparing performance of:
forEach vs for vs map vs while
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = []; let result = 0; for (var i = 0; i < 1000000; i++) { arr[i] = i; } function someFn(i) { result = i + 1; return result; } function fixResult() { console.log(result); }
Tests:
forEach
arr.forEach(function (item){ someFn(item); }) fixResult();
for
for (var i = 0, len = arr.length; i < len; i++) { someFn(arr[i]); } fixResult();
map
arr.map(item => someFn(item)) fixResult();
while
let i = 0, len = arr.length; while (i<len) { someFn(arr[i]); i++; } fixResult();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
forEach
for
map
while
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Related benchmarks:
Array loop vs foreach vs map (Small arrays)
Array loop vs foreach vs map -2
Array loop vs foreach vs map with large array
Fastest iteration over array: map vs forEeach vs while vs for loop
Fastest iteration over array: map vs forEeach vs while vs for loop (fixed)
Comments
Confirm delete:
Do you really want to delete benchmark?