Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
var for/for in/forEach array sum
(version: 0)
Comparing performance of:
for vs for each vs forEach
Created:
3 years ago
by:
Guest
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
let sum = 0; for (var i = 0; i < array.length; i++) { sum += array[i]; }
for each
let sum = 0; for (var item in array) { sum += item; }
forEach
let sum = 0; array.forEach(function(item, index) { sum += item; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for
for each
forEach
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 break down the benchmark and explain what is being tested. **Benchmark Overview** The benchmark measures the performance of three different ways to calculate the sum of an array in JavaScript: using a traditional `for` loop, `for...in`, and `forEach`. The test is designed to compare the performance of these approaches on a large array (10,000 elements). **Script Preparation Code** Before running the benchmarks, the script prepares an empty array with 10,000 elements and initializes its elements with their indices. **Html Preparation Code** The HTML preparation code is empty, which means that the benchmark is not dependent on any external factors like page loading or network latency. **Test Cases** There are three test cases: 1. **`for`**: This test uses a traditional `for` loop to iterate over the array and sum its elements. 2. **`for each`**: This test uses the `for...in` syntax to iterate over the array's properties (which correspond to its indices) and sum them up. 3. **`forEach`**: This test uses the `Array.prototype.forEach()` method, which is a callback-based iteration method, to iterate over the array and sum its elements. **Pros and Cons of Each Approach** Here are some pros and cons for each approach: * **`for`**: Pros: + Well-established and widely supported. + Can be optimized by the JavaScript engine (e.g., inlining). Cons: + May have slower performance compared to modern iteration methods like `forEach`. * **`for each`**: Pros: + Simple to implement and understand. Cons: + Not designed for iteration; it's meant for property access. + May cause issues if the array has non-enumerable properties or uses Object.prototype.toString() as its length property. + Performance may be slower compared to traditional `for` loops. * **`forEach`**: Pros: + Modern and widely supported (even in older browsers). + Encourages a more functional programming style. Cons: + May have performance overhead due to the callback function. + Can lead to memory leaks if not used carefully. **Library: Lodash** In one of the benchmark test cases, `for each` uses Lodash's `_.each()` method, which is not explicitly mentioned in the benchmark definition. Lodash is a popular JavaScript library that provides a set of utility functions for tasks like array iteration, string manipulation, and more. In this case, it's used as an external dependency to simplify the code and focus on the performance comparison. **Other Considerations** When writing benchmarks, consider the following: * Keep your benchmark as simple and focused as possible. * Use a consistent testing environment to minimize variability. * Warm up your test runner before running multiple iterations to reduce overhead. * Measure execution time consistently; use wall-clock time or more accurate timing methods like CPU ticks if necessary. **Other Alternatives** If you want to explore alternative iteration methods, consider: * `Array.prototype.map()` with a callback function * Using a loop variable and index in a traditional `for` loop (e.g., `let i = 0; array[i++];`) * Using the `reduce()` method (not shown in this benchmark) Keep in mind that each approach has its own trade-offs, and you should choose the one that best fits your specific use case.
Related benchmarks:
for of vs Array.reduce vs Array.forEach vs for i for summing and array of integers
for/for each/forEach array sum
for/for in/forEach array sum
var len for/for in/forEach array sum
Comments
Confirm delete:
Do you really want to delete benchmark?