Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.add vs array.push more data
(version: 1)
Comparing performance of:
array.push vs set.add
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
array.push
const a = [] for (let i = 0; i < 30000; i++) a.push(i)
set.add
const s = new Set() for (let i = 0; i < 30000; i++) s.add(i)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array.push
set.add
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Browser/OS:
Chrome 141 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.push
13311.7 Ops/sec
set.add
1132.5 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON compares the performance of two different data structure methods in JavaScript: `array.push` and `set.add`. Specifically, it measures how many times each method can be executed in one second. ### Options Compared 1. **array.push**: - **Benchmark Definition**: This code initializes an empty array and pushes 30,000 integers into it using a for loop. - **Pros**: - **Speed**: As evidenced by the benchmark results, `array.push` is significantly faster (16,084.02 executions per second). - **Simplicity**: The array is a very simple and commonly used data structure in JavaScript, which is easy to understand and manipulate. - **Cons**: - **Memory Management**: Large arrays can lead to higher memory consumption since they may need to be resized if the specified capacity is exceeded. - **Order Preservation**: While preserving order can be an advantage, it also means that searching, removing, or looking up elements may be slower since it requires traversing the elements. 2. **set.add**: - **Benchmark Definition**: This code initializes a new `Set` and adds 30,000 integers to this set. - **Pros**: - **Uniqueness**: A `Set` automatically enforces uniqueness of its elements. If you try to add a duplicate, it will not be added, making it optimal for scenarios where unique values are required. - **Optimal Lookup**: Sets provide faster lookups than arrays when checking for the existence of an element. - **Cons**: - **Performance**: As seen in the results, `set.add` is much slower (1,292.01 executions per second) compared to `array.push`. - **Extra Overhead**: Sets may have a higher overhead for memory management compared to simple arrays, especially if the sole requirement is simply storing sequential data. ### Summary of Results The execution speed comparison clearly shows that, for this specific operation (adding a series of integers), the `array.push` method outperforms `set.add` by a considerable margin. ### Considerations and Alternatives - If the application requires the storage of only unique values, sets would be the better choice despite the slower performance in this particular benchmark. - For mixed operations involving unique checks along with ordered collections, some alternatives to both could be: - **Maps**: If key-value pairs are needed with unique keys, a `Map` could provide similar functionality while allowing efficient lookups. - **Flat Sets**: Depending on the context, a flattened array might work well for small and known data sizes where performance is critical. In situations where interactivity and data manipulation are more dynamic (like frequently changing datasets), choosing between arrays and sets would depend heavily on the specific requirements in terms of performance, memory consumption, and usability within the broader application context. Different operations (like searching, deleting, or iterating over data) will have varying performance implications based on the chosen structure.
Related benchmarks:
set.add vs array.push
set.has vs. array.includes large 1
Array of nulls
fill vs push
fill + map vs push
add vs object
array.push() VS Set.add()
set.add vs array.push vc1
set.add vs array.push small size - 30 elements
Comments
Confirm delete:
Do you really want to delete benchmark?