Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set vs array iterationgfgdhahtfdgcvfgshjrhdgfd
(version: 1)
Comparing performance of:
array vs set
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const a = []; for (let i = 0; i < 10000; i++) { a.push(i); }; const b = new Set(a)
Tests:
array
for (const x of a) { void x; };
set
for (const x of b) { void x; };
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 year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array
83673.8 Ops/sec
set
22134.4 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON is comparing the performance of iterating over two different data structures in JavaScript: an array and a set. The specific operations being measured are the time taken to iterate through each structure. ### Benchmark Structure 1. **Preparation Code**: - An array `a` is created containing the integers from 0 to 9999 (inclusive) using a simple for-loop. - A `Set` object `b` is then created from the array `a`. In JavaScript, a `Set` is a built-in object that holds a collection of unique values, meaning it automatically removes duplicates. 2. **Test Cases**: - The benchmark runs two tests: - **Array iteration**: Using a `for...of` loop to iterate over the elements in the array `a`. - **Set iteration**: Using a `for...of` loop to iterate over the unique values stored in the set `b`. ### Performance Results The results from the benchmark indicate that the array iteration outperforms the set iteration significantly. The "ExecutionsPerSecond" metric shows: - Array iteration: **83,673.84** executions per second - Set iteration: **22,134.39** executions per second ### Pros and Cons of Each Approach #### Array Iteration: - **Pros**: - Faster iteration for the given benchmark, as shown in the results. - Simple and straightforward syntax, making it easy to understand and use in general programming contexts. - Arrays maintain the order of elements, which is beneficial when the sequence of inputs matters. - **Cons**: - Arrays can contain duplicate values, which may lead to unexpected results if uniqueness is required. - Performance might degrade with larger datasets, particularly if manipulation (insertion, deletion) is frequent. #### Set Iteration: - **Pros**: - Automatically enforces uniqueness, ensuring only distinct values are stored. - Potentially more efficient for certain operations that involve checking for existence (i.e., `has` method) compared to arrays. - Sets may provide better performance in scenarios where frequent membership checks are necessary. - **Cons**: - Slower iteration performance compared to arrays in this specific benchmark. - May require more memory overhead to maintain uniqueness compared to an array, particularly with a large number of elements. - Does not maintain element order as well as arrays, which may be a disadvantage if the order is significant. ### Other Considerations - When deciding between using an array or a set, the structure of your data and the operations that will be performed on it should be carefully considered. If you need rapid iteration and order, an array is preferable; if you need uniqueness and frequent membership checks, a set may be the better choice. - For large datasets, the choice of data structure can greatly influence performance, and using appropriate benchmarking tools like MeasureThat.net helps developers understand the trade-offs in different environments. - Alternatives to these structures include using maps for key-value pairs or employing custom data structures depending on specific application needs. Overall, the benchmark highlights important aspects of performance when working with built-in JavaScript data structures, helping developers make informed decisions when developing applications.
Related benchmarks:
For loop initialization.
recreate array vs set
recreate array vs set 2
forEach vs for ... of
set vs array creation
DDS Array vs Set - performance v4
set vs array iteration 100kf
set vs array iteration for,for-of and values v3
set vs array vs map iteration for,for-of and values with init
Comments
Confirm delete:
Do you really want to delete benchmark?