Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For of with entries vs basic for
(version: 0)
Comparing performance of:
for of entries vs for
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array.from({length: 1_000_000}).fill(0).map(() => Math.random())
Tests:
for of entries
const res = [] for (const [i, val] of arr.entries()) res.push(i+val)
for
const res = [] for (let i = 0; i < arr.length; i++) res.push(i+arr[i])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for of entries
for
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0
Browser/OS:
Firefox 122 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for of entries
48.7 Ops/sec
for
187.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this JavaScript microbenchmark. **Benchmark Overview** The benchmark compares the performance of two loops: `for` and `for...of with entries`. Both loops are used to iterate over an array and perform a simple calculation on each element. The goal is to determine which loop is faster. **Loop Implementations** There are two test cases: 1. **`for` Loop**: This loop uses a traditional `for` loop syntax, where the index `i` is explicitly incremented at each iteration. ```js const res = []; for (let i = 0; i < arr.length; i++) { res.push(i + arr[i]); } ``` 2. **`for...of with entries` Loop**: This loop uses a modern syntax that iterates over an array using the `entries()` method, which returns an iterator object. ```js const res = []; for (const [i, val] of arr.entries()) { res.push(i + val); } ``` **Options Compared** The two loops are compared in terms of performance, specifically the number of executions per second. **Pros and Cons of Each Approach** 1. **Traditional `for` Loop**: * Pros: Easy to understand and implement, works well for arrays with a small number of elements. * Cons: Can be slow for large arrays, as the index incrementation can lead to cache misses. 2. **`for...of with entries` Loop**: * Pros: More concise and expressive syntax, uses iterator objects under the hood, which can improve performance for large datasets. * Cons: May require more memory due to the creation of an iterator object, and can be slower than traditional loops for small arrays. **Library Used** In this benchmark, no external libraries are used. **Special JavaScript Features or Syntax** The `for...of with entries` loop uses a modern syntax feature introduced in ECMAScript 2015 (ES6). This syntax is designed to make working with iterables more concise and expressive. **Other Considerations** When choosing between these two loops, consider the size of your dataset and the performance requirements of your application. If you're working with small arrays or require low latency, the traditional `for` loop might be sufficient. However, if you need to process large datasets efficiently, the `for...of with entries` loop could be a better choice. **Alternative Loops** Other alternative loops that might be used in this benchmark include: * **`forEach` Method**: This method is often used for array iteration, but it's not as efficient as the traditional `for` loop or `for...of with entries` loop. * **`map` Method**: While the `map` method can be used to create a new array by iterating over an existing one, it's not as flexible as the `for...of with entries` loop. Keep in mind that these alternatives might have their own trade-offs and performance characteristics.
Related benchmarks:
Fill array with random integers
Right shift VS Divide and round
Array fill method vs for loop
new Array() vs Array.from() with random data
Array.from VS spreading for
Comments
Confirm delete:
Do you really want to delete benchmark?