Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
different ways to loop
(version: 0)
Comparing performance of:
for vs for-of
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [...new Array(10000)].map(() => Math.random());
Tests:
for
let sum = 0; for (let i = 0; i < arr.length; i++) { sum += arr[i]; }
for-of
let sum = 0; for (const x of arr) { sum += x; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for
for-of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0
Browser/OS:
Firefox 124 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
58879.0 Ops/sec
for-of
14399.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The `Script Preparation Code` section defines an array of 10,000 random numbers, which will be used as input for the loop tests. **Test Cases** There are two test cases: 1. **"for"`**: This test case uses a traditional `for` loop to iterate over the array and sum up its elements. 2. **"for-of"`**: This test case uses an enhanced `for...of` loop to iterate over the array and sum up its elements. **Loop Comparison** The benchmark compares the execution performance of these two different loop approaches on a desktop browser (Firefox 124) running on Mac OS X 10.15. Now, let's discuss the pros and cons of each approach: **Traditional `for` Loop:** Pros: * More control over iteration variables * Can be more efficient for specific use cases Cons: * Requires manual incrementation of the loop variable * Can lead to magic numbers (e.g., hardcoding the array length) **Enhanced `for...of` Loop:** Pros: * Less verbose and easier to read * Eliminates the need for explicit loop variable management * Automatically handles iteration limits Cons: * May have performance overhead due to additional indirections * Can be less efficient in certain situations (e.g., when using a custom iterator) **Other Considerations:** * Both loops use the same array and calculation logic, which helps isolate the effects of loop syntax. * The benchmark doesn't consider other factors that might impact performance, such as: + Cache efficiency + Branch prediction + SIMD (Single Instruction, Multiple Data) instructions Regarding libraries or special JavaScript features, there are none mentioned in this specific benchmark. **Other Alternatives:** For similar benchmarks, you could explore: * Iterators and generators (e.g., using `for...of` with an iterator) * Loop unrolling techniques * Using optimized loop algorithms (e.g., for binary search or Fibonacci sequences) In summary, this benchmark compares the performance of traditional `for` loops versus enhanced `for...of` loops in a simple iteration scenario.
Related benchmarks:
Fill array with random integers
Array .push() vs .unshift() with random numbers
Array loop vs foreach vs map2
Flatten Array of Arrays
Array.from VS spreading for
Comments
Confirm delete:
Do you really want to delete benchmark?