Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
sum for vs foreach vs some vs for..of vs for with cached length
(version: 1)
Compare loop performance doing summation
Comparing performance of:
for vs foreach vs some vs for..of vs for with cached length
Created:
9 months ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100); for (let i = 0, end = array.length; i < end; i += 1) { array[i] = i; }
Tests:
for
let sum = 0; for (var i = 0; i < array.length; i += 1) { sum += array[i]; }
foreach
let sum = 0; array.forEach(function(i) { sum += array[i]; });
some
let sum = 0; array.some(function(i) { sum += array[i]; });
for..of
let sum = 0; for (var i of array) { sum += array[i]; }
for with cached length
let sum = 0; for (var i = 0, end = array.length; i < end; i += 1) { sum += array[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
for
foreach
some
for..of
for with cached length
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
4947943.5 Ops/sec
foreach
3822214.0 Ops/sec
some
3799700.2 Ops/sec
for..of
4279565.5 Ops/sec
for with cached length
6645368.5 Ops/sec
Related benchmarks:
for vs foreach vs some vs for..of 1000 (improved)
for vs forEach vs for-of vs for-reverse vs reduce (calculate sum)
for/for each/forEach array sum
for/for in/forEach array sum
var for/for in/forEach array sum
var len for/for in/forEach array sum
Sum array: for vs forEach vs for..in vs for..of vs reduce
2Sum array: for vs forEach vs for..in vs for..of vs reduce
for vs foreach vs for..in vs for..of sum
Comments
Confirm delete:
Do you really want to delete benchmark?