Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set vs array iteration
(version: 0)
Comparing performance of:
array vs set
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var b = new Set(a)
Tests:
array
for (const x of a) {}
set
for (const x of b) {}
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array
set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array
162496544.0 Ops/sec
set
87447760.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided benchmark. **Benchmark Purpose** The benchmark measures the performance difference between iterating over an array and iterating over a Set (a data structure that automatically removes duplicates) using a `for...of` loop. **Comparison Options** 1. **Array Iteration**: The first option uses a traditional array (`var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`) and iterates over its elements using a `for...of` loop. 2. **Set Iteration**: The second option uses a Set created from the array (`var b = new Set(a)`) and iterates over its elements using another `for...of` loop. **Pros and Cons of Each Approach** * **Array Iteration**: + Pros: More straightforward, efficient for small to medium-sized datasets. + Cons: Can be slower for large datasets due to the overhead of searching for duplicate values in the Set. * **Set Iteration**: + Pros: Efficiently removes duplicates, potentially faster for large datasets since it doesn't require searching for duplicates. + Cons: Requires more memory to store the Set, and iterating over a Set can be slower than iterating directly over an array. **Library Used (if applicable)** In this benchmark, no specific library is used besides JavaScript's built-in `Set` object. However, in general, when working with large datasets or performance-critical code, libraries like `lodash` or `fast-sets` might be used to optimize iteration and data processing operations. **Special JS Features/Syntax (if applicable)** The benchmark uses the `for...of` loop syntax, which is a modern JavaScript feature introduced in ECMAScript 2015. This allows for concise iteration over arrays, Sets, or other iterable objects without the need for traditional indexing. **Other Alternatives** For alternative approaches to iterating over arrays and Sets, consider: * Using `Array.prototype.forEach()` or `Set.prototype.forEach()` instead of `for...of` loops. * Utilizing libraries like `lodash` for optimized array and Set operations. * Exploring other data structures, such as `Map` or `WeakSet`, which offer different iteration patterns. Keep in mind that the choice of iteration approach depends on the specific requirements of your project, including performance considerations, memory usage, and code readability.
Related benchmarks:
Array.from vs. ... expansion
set vs array find if exists
set vs array iteration new new
3set vs array iteration New doge333
Comments
Confirm delete:
Do you really want to delete benchmark?