Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
NodeList For vs Foreach
(version: 0)
Comparing performance of:
Default For vs For with length caching vs forEach
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var div = document.createElement('div'); for (let i = 0; i < 1000; i++) { div.insertAdjacentHTML('beforeend', `<span>span-${i}<span>`); } var list = div.querySelectorAll('span');
Tests:
Default For
for(let i = 0; i < list.length; i++) { list[i].setAttribute('tabindex', '-1'); }
For with length caching
list.forEach(item =>{ item.setAttribute('tabindex', '-1'); });
forEach
for(let i = 0, lenght = list.lenght ; i < length; i++) { list[i].setAttribute('tabindex', '-1'); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Default For
For with length caching
forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Default For
3379.8 Ops/sec
For with length caching
2192.3 Ops/sec
forEach
5767589.0 Ops/sec
Related benchmarks:
NodeList for vs forEach vs length cached
NodeList For vs Foreach-
NodeList For vs Foreach1231
NodeList For vs Foreach fix
Comments
Confirm delete:
Do you really want to delete benchmark?