Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs some vs for...of
(version: 0)
Compare loop performance with less JIT interference
Comparing performance of:
for vs foreach vs some vs for...of vs for...in vs foreach key
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var array = new Array(10000); for(let i = 0; i < array.length; i++) { array[i] = i; }
Tests:
for
var a = 0; for(let i = 0; i < array.length; i++) { a += array[i]; } console.log(a);
foreach
var a = 0; array.forEach(el => {a += el}); console.log(a);
some
var a = 0; array.some(el => {a += el}); console.log(a);
for...of
var a = 0; for(let i of array) { a += i; } console.log(a);
for...in
var a = 0; for(let i in array) { a += array[i]; } console.log(a);
foreach key
var a = 0; array.forEach((el, key) => {a += array[key]}); console.log(a);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
for
foreach
some
for...of
for...in
foreach key
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
715.4 Ops/sec
foreach
69876.1 Ops/sec
some
49359.8 Ops/sec
for...of
98899.7 Ops/sec
for...in
1213.5 Ops/sec
foreach key
1278.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of what is being tested in this benchmark. **Overview** The provided benchmark compares the performance of different loop constructs in JavaScript: `for`, `foreach`, `some`, and `for...of`. The goal is to measure how much each approach interferes with the Just-In-Time (JIT) compiler, which can impact performance. **Options Compared** Here's a brief explanation of each option: 1. **`for` loop**: A traditional loop construct that uses an index variable (`i`) to iterate over an array. 2. **`foreach` loop**: A loop construct introduced in ECMAScript 2015 (ES6) that iterates over an array using a callback function for each element. 3. **`some` loop**: Another loop construct introduced in ES6, which returns `true` as soon as the callback function returns `true`. 4. **`for...of` loop**: A loop construct introduced in ES6, which allows iterating over arrays and other iterable objects using a simple syntax similar to `foreach`. **Pros and Cons** Here's a brief summary of each approach: * **`for` loop**: Simple, widely supported, but may have performance implications due to JIT interference. + Pros: Easy to understand, well-supported by most browsers. + Cons: May not be as efficient as other options. * **`foreach` loop**: Introduced in ES6, which can lead to better performance due to reduced JIT overhead. + Pros: Good performance, modern syntax. + Cons: Less widely supported than `for`. * **`some` loop**: Another recent addition to the JavaScript standard library, which can be fast but may not work well with all use cases. + Pros: Fast, concise syntax. + Cons: May have limitations and compatibility issues. * **`for...of` loop**: Similar to `foreach`, but provides a more modern syntax for iterating over arrays and other iterables. + Pros: Easy to read, good performance. + Cons: Less widely supported than `foreach`. **Library** None of the provided benchmark cases use any external libraries. **Special JS Features/Syntax** The benchmark includes two special features: 1. **`for...in` loop**: A legacy loop construct that iterates over an object's own enumerable properties, not arrays. + This option is skipped in the provided benchmark since it targets a different use case (iterating over objects). 2. **Callback functions with `key` parameter**: Some `foreach` loops include a callback function with a `key` parameter, which can be used to access both the current element and its index. + While this feature is not specific to loop performance, it's included in some benchmark cases to test its impact. **Other Alternatives** If you're interested in exploring other loop options or optimizing JavaScript performance, consider looking into: 1. **`while` loops**: A more traditional approach that can be faster than `for` loops in some cases. 2. **`map()`, `filter()`, and `reduce()` methods**: These array methods can simplify loop logic and often have better performance due to optimized implementations. 3. **Async iterations**: Consider using async iterators, which allow writing efficient asynchronous code that's more readable and maintainable. Keep in mind that the best approach will depend on your specific use case, performance requirements, and the trade-offs you're willing to make.
Related benchmarks:
for vs foreach vs some vs for..of vs for cached
foreach vs for..of
for vs foreach for (cached length) vs for..of
For loop vs <Array>.forEach() vs for...of loop
for vs foreach vs for..of vs for..of over entries vs for in
Comments
Confirm delete:
Do you really want to delete benchmark?