Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
javascript loops performance
(version: 0)
test js each loops performance
Comparing performance of:
for vs for_in vs for_of vs forEach vs map vs while
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = new Array(100000).fill(Math.random()); var lastValue = 0;
Tests:
for
for (var i = 0; i < arr.length; i++) { lastValue = arr[i]; }
for_in
for (var i in arr) { lastValue = arr[i]; }
for_of
for (var v of arr) { lastValue = v; }
forEach
arr.forEach(v => lastValue = v);
map
arr.map(v => lastValue = v);
while
let i = 0; while(i < arr.length) { lastValue = arr[i]; i++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
for
for_in
for_of
forEach
map
while
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 its components. **Benchmark Purpose:** The goal of this benchmark is to compare the performance of different loop structures in JavaScript, specifically: * `for` loops * `for...in` loops (which iterate over an object's properties) * `for...of` loops (which iterate over an array or other iterable) * `forEach` * `map` **Loop Structures Compared:** The benchmark compares the performance of these five loop structures on a specific test case: iterating over an array and assigning a value to a variable (`lastValue`) in each iteration. **Options Compared:** 1. **`for` loops**: Iterate manually using `i = 0; while(i < arr.length) { ... }`. 2. **`for...in` loops**: Iterate over the array's properties, but assign the value to `lastValue` directly. 3. **`for...of` loops**: Iterate over the array and assign the value to `lastValue` in each iteration. 4. **`forEach`**: Use the `forEach` method to iterate over the array and execute a callback function for each element. 5. **`map`**: Use the `map` method to create a new array with the same number of elements as the original array, but this time also assign the value to `lastValue` in each iteration. **Pros and Cons:** 1. **`for` loops**: * Pros: Simple and well-understood. * Cons: Can be error-prone if not implemented correctly (e.g., forgetting to increment `i`). 2. **`for...in` loops**: * Pros: Convenient for iterating over objects, but can produce unexpected results when iterating over arrays. * Cons: May iterate over the array's prototype chain or other properties. 3. **`for...of` loops**: * Pros: Modern and efficient way to iterate over arrays. * Cons: Requires using a variable (`v`) to store each value, which can be slower than direct assignment. 4. **`forEach`**: * Pros: Convenient for iterating over arrays and executing a callback function. * Cons: May not be as efficient as other options (e.g., `for...of` loops) due to the overhead of the callback function. 5. **`map`**: * Pros: Useful for creating new arrays with transformed data, but can produce unexpected results if not used correctly. * Cons: Iterates over the original array and produces additional overhead due to the map operation. **Library Used:** The `forEach` and `map` methods are part of the JavaScript language itself, so no libraries are required for these options. **Special JS Feature/Syntax:** There is no special JS feature or syntax used in this benchmark. The focus is on comparing different loop structures and their performance characteristics. **Alternatives:** * **Other iteration methods**: Other iteration methods, such as `while` loops with manual indexing (`arr[i]`) or using `Array.prototype.reduce()`, could also be included in the benchmark to compare their performance. * **Native array operations**: Native array operations like `reduce()` or `every()` could be used instead of traditional loops to create a more modern and efficient comparison. * **Async iterations**: Asynchronous iterations, such as using `async/await` with a callback function, could be added to the benchmark to simulate real-world scenarios where performance differences are critical.
Related benchmarks:
Fill array with random integers
Array .push() vs .unshift() with random numbers
.at vs [x]
array math.max vs for loop
Array fill method vs for loop
Comments
Confirm delete:
Do you really want to delete benchmark?