Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
looping test
(version: 1)
Comparing performance of:
for loop vs foreach loop
Created:
10 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const styleElement = 666; const mutations = [{ removedNodes: [styleElement], }, ]; for (let i = 0; i < 1000; i++) { for (let j = 0; j < 1000; j++) { if (j === 0) mutations[i] = { removedNodes: [] }; mutations[i]?.removedNodes.push(j); } }
Tests:
for loop
outer: for (let i = 0; i < mutations.length; i++) { const mutation = mutations[i]; const removedNodes = mutation.removedNodes; for (let j = 0; j < removedNodes.length; j++) { const node = removedNodes[j]; if (node === styleElement) { console.log('styleElement removed'); break outer; // Breaks out of both loops } } }
foreach loop
mutations.forEach((mutation) => { mutation.removedNodes.forEach((node) => { if (node === styleElement) { console.log('styleElement removed'); } }); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for loop
foreach loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for loop
248891.8 Ops/sec
foreach loop
242.3 Ops/sec
Related benchmarks:
Lodash Uniq vs Javascript Set (lol4)
For Loop Different Approaches
On2 vs Onlogn lengthOfLIS
loop vs map 1994
3333test
Arrays Destructuring
Array timing
Array timing 100
100 array
Comments
Confirm delete:
Do you really want to delete benchmark?