Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS loops
(version: 0)
Comparing performance of:
for loop vs forEach loop vs for...of loop
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array1 = new Array(10000).fill(1).map((v, i) => i);
Tests:
for loop
for (var i = 0; i < array1.length; i++) { var el = array1[i]; var result = el * 2; }
forEach loop
array1.forEach((el) => { var result = el * 2; });
for...of loop
for (var el of array1) { var result = el * 2; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for loop
forEach loop
for...of loop
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):
**Overview of the Benchmark** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON data represents a benchmark that compares the performance of three different types of loops: traditional `for` loop, `forEach` loop, and `for...of` loop. **Loop Options Compared** The benchmark tests three loop options: 1. **Traditional `for` Loop**: This is the most basic type of loop in JavaScript. It uses a variable (`i`) to iterate over an array. 2. **`forEach` Loop**: This loop is used for iterating over arrays and objects. In this case, it's used to multiply each element of the `array1` by 2. 3. **`for...of` Loop**: This is a newer type of loop in JavaScript that's designed for iterating over arrays. It uses a simple syntax to iterate over elements. **Pros and Cons of Each Approach** Here are some pros and cons of each approach: * **Traditional `for` Loop**: + Pros: Simple, efficient, and widely supported. + Cons: Can be verbose, especially when working with large datasets. * **`forEach` Loop**: + Pros: Concise, expressive syntax, and suitable for iterating over arrays. + Cons: Not suitable for controlling iteration (e.g., looping `i` times). * **`for...of` Loop**: + Pros: Concise, modern syntax, and efficient. + Cons: Requires JavaScript 2015 or later to run. **Library and Special JS Features** In this benchmark, the `map()` function is used in both the traditional `for` loop and the `forEach` loop. The `map()` function is a built-in JavaScript method that creates a new array by applying a given function to each element of an existing array. No special JavaScript features or syntax are mentioned in the provided data. **Other Alternatives** If you're interested in exploring other loop options, here are some alternatives: * **While Loop**: An older type of loop that uses a condition to control iteration. * **Do...While Loop**: A variation of the `while` loop that executes the loop body at least once before checking the condition. * **For-In Loop**: Used for iterating over object properties (not arrays). * **Array.prototype.reduce()` and `Array.prototype.findIndex()`: Higher-order functions that can be used for looping over arrays. **Conclusion** The provided benchmark highlights the importance of choosing the right loop type for your specific use case. Understanding the pros and cons of each approach will help you write more efficient, readable, and maintainable JavaScript code.
Related benchmarks:
for vs foreach vs map 2
fill vs map
Array fill method vs for loop vs map
Array fill map, vs for i loop
Comments
Confirm delete:
Do you really want to delete benchmark?