Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for..of
(version: 0)
Comparing performance of:
0 vs 1 vs 2 vs 3 + get link vs 4 + get link
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = new Array(10000).map(()=>Math.random())
Tests:
0
var sum=''; for(const item of arr){ sum+=item; }
1
var sum=''; for(let i=0; i<arr.length; i++){ sum+=arr[i]; }
2
var sum=''; for(let i=0, len=arr.length; i<len; i++){ sum+=arr[i]; }
3 + get link
var sum=''; let arrs = arr; for(let i=0, len=arrs.length; i<len; i++){ sum+=arrs[i]; }
4 + get link
var sum=''; let arrs = arr; for(const item of arrs){ sum+=item; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
0
1
2
3 + get link
4 + get link
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):
The provided JSON represents a benchmark test for the `for..of` loop in JavaScript. The goal of this test is to compare the performance of different variations of the `for..of` loop. **Benchmark Definition** In the "Benchmark Definition" section, we have four different versions of the `for..of` loop: 1. `for (const item of arr) { sum += item; }` 2. `for (let i = 0; i < arr.length; i++) { sum += arr[i]; }` 3. `for (let i = 0, len = arr.length; i < len; i++) { sum += arr[i]; }` 4. `let arrs = arr; for (const item of arrs) { sum += item; }` **Options Compared** The test compares the performance of these four different versions of the `for..of` loop: * **Version 1**: Uses the `for...of` loop with a const declaration, which allows it to iterate over an array without a fixed index. * **Version 2**: Uses the traditional `for` loop with a let declaration and indexing into the array using square brackets (`arr[i]`). * **Version 3**: Uses the traditional `for` loop with a let declaration and a separate variable for the array length (`len = arr.length; i < len; i++`). * **Version 4**: Assigns the array to a new variable (`let arrs = arr`) before using it in the `for...of` loop. **Pros and Cons** Here are some pros and cons of each approach: 1. **Version 1**: * Pros: More concise and expressive code, avoids indexing into the array. * Cons: May not be as readable for those unfamiliar with `for...of`. 2. **Version 2**: * Pros: Widely supported and well-known syntax, easy to read and write. * Cons: Requires indexing into the array, which can lead to off-by-one errors. 3. **Version 3**: * Pros: Avoids the need for indexing into the array, but requires an extra variable for the length. * Cons: May be less readable due to the additional variable, and is less common than other approaches. 4. **Version 4**: * Pros: Similar to Version 1, but with a temporary assignment for better readability. * Cons: Requires assigning the array to a new variable, which can lead to unnecessary memory allocation. **Library Use** There are no libraries used in this benchmark test. **Special JS Features or Syntax** The `for...of` loop is a built-in JavaScript feature that allows iterating over arrays and other iterable objects without a fixed index. It was introduced in ECMAScript 2015 (ES6). **Other Alternatives** If you're looking for alternatives to the `for...of` loop, you can consider: * **Traditional `for` loop**: As shown in Version 2. * **Array methods**: Such as `map()`, `forEach()`, or `reduce()`, which can simplify iteration over arrays but may not be suitable for all use cases. In summary, the test compares four different variations of the `for...of` loop in JavaScript, each with its pros and cons. The choice of approach depends on the specific use case and personal preference.
Related benchmarks:
Fill array with random integers
Array .push() vs .unshift() with random numbers
.at vs [x]
Array push or set
Array.from VS spreading for
Comments
Confirm delete:
Do you really want to delete benchmark?