Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs forEach() vs for...of
(version: 18)
Compare loop performance
Comparing performance of:
for vs foreach vs for..of
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const testArray = new Array(10000).fill({ object: 'with', some: 'data', }); var testFor = () => { const len = testArray.length; for (let i = 0; i < len; i += 1) { const { object, } = testArray[i]; } }; var testForEach = () => { testArray.forEach((item) => { const { object, } = item; }); }; var testForOf = () => { for (const item of testArray) { const { object, } = item; } }; var bench = (func, count = 1) => { for (let i = 0; i < count; i += 1) { func(); }; };
Tests:
for
testFor()
foreach
testForEach()
for..of
testForOf()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for
foreach
for..of
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Overview** The provided benchmark compares the performance of three loop constructs in JavaScript: `for`, `forEach()`, and `for...of`. The goal is to measure which construct performs best in terms of execution speed. **Loop Constructs Compared** 1. **`for`**: This traditional loop construct uses a manual index variable (`i`) to iterate over an array. 2. **`forEach()`**: This method allows iterating over an array without explicitly indexing into it, using a callback function to process each element. 3. **`for...of`**: This modern loop construct was introduced in ECMAScript 2015 (ES6) and is designed for simplicity and readability. It uses a iterator object to iterate over arrays or other iterable objects. **Pros and Cons of Each Approach** 1. **`for`**: * Pros: Well-established, easy to understand, and suitable for complex logic. * Cons: Requires manual index management, which can lead to off-by-one errors. 2. **`forEach()`**: * Pros: Convenient, straightforward syntax, and no need to worry about indexing. * Cons: Can be less efficient due to the overhead of the callback function and potential memory allocation. 3. **`for...of`**: * Pros: Easy to read and write, with a more modern and expressive syntax. * Cons: May have performance issues if not optimized correctly (e.g., using unnecessary iterator creation). **Library Usage** The benchmark script uses the `testArray` object, which is initialized as an array of 10,000 objects. This indicates that the script is designed to test iteration over large arrays. **Special JavaScript Feature/Syntax** None are explicitly mentioned in this benchmark. **Benchmark Preparation Code Explanation** The script prepares three functions: * `testFor()`: Demonstrates a traditional `for` loop using an index variable. * `testForEach()`: Shows how to use the `forEach()` method to iterate over the array. * `testForOf()`: Illustrates the use of the `for...of` loop construct. The `bench` function is used to execute each test function a specified number of times (defaulting to 1). **Latest Benchmark Results** The results show that the order of execution per second for the three loops is: 1. `forEach()`: 265,920 executions/second 2. `for...of`: 177,743 executions/second 3. `for`: 212,911 executions/second This indicates that, according to this benchmark, `forEach()` has a slight performance advantage over the other two options. **Other Alternatives** If you're interested in exploring more loop constructs or optimizing your code for better performance, consider the following alternatives: * Other ES6+ features like `map()`, `filter()`, and `reduce()`, which can provide efficient and concise ways to process arrays. * Third-party libraries like Lodash or Ramda, which offer a wide range of utility functions for array manipulation and iteration. Keep in mind that performance differences between loop constructs and other optimizations may be negligible unless you're working with extremely large datasets or performance-critical applications.
Related benchmarks:
for (cached length) vs foreach vs some
for vs foreach vs for..of vs for..of over entries (const)
for-in vs foreach 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?