Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Looping With Stuff
(version: 1)
Loop some stuff haha
Comparing performance of:
Forof vs ForEach vs For
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
// Generate a large dataset (1 million numbers) const largeDataset = Array.from({ length: 1_000_000 }, (_, i) => i + 1); // Function using for...of loop function testForOfLoop(arr) { console.time("for...of loop"); let sum = 0; for (const num of arr) { if (num === 500_000) continue; // Skip 500,000 if (num === 900_000) break; // Stop at 900,000 sum += num; } console.timeEnd("for...of loop"); return sum; } // Function using forEach loop function testForEachLoop(arr) { console.time("forEach loop"); let sum = 0; arr.forEach((num) => { if (num === 500_000) return; // Doesn't break out if (num === 900_000) return; // Still loops sum += num; }); console.timeEnd("forEach loop"); return sum; } // Function using for loop function testForLoop(arr) { console.time("for loop"); let sum = 0; for (let i = 0; i < arr.length; i++) { if (arr[i] === 500_000) continue; // Skip 500,000 if (arr[i] === 900_000) break; // Stop at 900,000 sum += arr[i]; } console.timeEnd("for loop"); return sum; }
Tests:
Forof
testForOfLoop(largeDataset)
ForEach
testForEachLoop(largeDataset)
For
testForLoop(largeDataset)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Forof
ForEach
For
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/118.0.0.0 Safari/537.36
Browser/OS:
Chrome 118 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Forof
90.2 Ops/sec
ForEach
59.2 Ops/sec
For
492.4 Ops/sec
Related benchmarks:
Loop Optimization
Loop Optimization
Loop Optimization
For vs For of loop
Loop Optimization Working
generator vs array
traditional for loop vs for ... of
Range vs for of
forloop performance test
Comments
Confirm delete:
Do you really want to delete benchmark?