Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Iterate-Set-vs-arr
(version: 0)
Iterate over a set.
Comparing performance of:
for of: many items vs convert to array: many items vs native iterator: many items vs native iterator w/ size: many items:
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var manyItemSet = new Set(Array.from({ length: 10000 }, (_, i) => i)); var arr = Array.from(manyItemSet);
Tests:
for of: many items
let result = 0; for (const item of manyItemSet) { result += item; }
convert to array: many items
let result = 0; let l = arr.length; for (let i = 0; i < l; i++) { result += arr[i]; }
native iterator: many items
let result = 0; let val = null; const iter = manyItemSet.values(); while (!(val = iter.next()).done) { result += val.value; }
native iterator w/ size: many items:
let result = 0; let iter = manyItemSet.values(); for (let i = manyItemSet.size; i--;) { result += iter.next().value; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for of: many items
convert to array: many items
native iterator: many items
native iterator w/ size: many items:
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 provided JSON and explain what's being tested. **Benchmark Overview** The benchmark compares the performance of three ways to iterate over a set of 10,000 items: 1. **For-of loop**: Using the `for...of` loop with an iterator. 2. **Converting Set to Array**: Converting the set to an array and then iterating using a traditional for loop. 3. **Native Iterator**: Using a native iterator (via `values()`) directly on the set. **Options Comparison** The three options are compared in terms of performance, which is measured by the number of executions per second. **Pros and Cons:** 1. **For-of loop**: * Pros: More readable, concise, and efficient for small to medium-sized datasets. * Cons: May not be as efficient as other methods for very large datasets, as it requires creating an iterator object. 2. **Converting Set to Array**: * Pros: Easy to understand, but can lead to poor performance due to the unnecessary conversion step. * Cons: Inefficient use of memory and resources, especially for large datasets. 3. **Native Iterator**: * Pros: Optimized for iteration over sets, using native code and avoiding unnecessary conversions. * Cons: May require more expertise to set up correctly. **Library Used** None. **Special JS Feature/Syntax** The `for...of` loop and the use of iterators are standard JavaScript features. The native iterator method uses the `values()` method, which is a part of the ECMAScript 2015 (ES6) specification. **Other Alternatives** If you need to iterate over sets in other ways, consider using: * `forEach()`: A more concise alternative to traditional for loops. * `entries()`, `keys()`, or `values()` on Sets: Depending on the specific use case, these methods can be used as alternatives to native iterators. **Benchmark Preparation Code** The provided preparation code creates a large set of 10,000 items and converts it to an array for subsequent iterations. This ensures that all three iteration methods have access to the same dataset. Keep in mind that this benchmark is specific to iterating over sets using JavaScript's `for...of` loop, converting to arrays, and native iterators. Different benchmarks might focus on different aspects or use cases.
Related benchmarks:
Array loop vs foreach vs map (large set)
Array loop vs foreach vs map (really large set)
`Array.slice(-1)[0]` vs `Array[Array.length]` for 10000 length
Array loop: foreach vs map
Array map vs forEach
Comments
Confirm delete:
Do you really want to delete benchmark?