Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.add vs array.push small size - 30 elements
(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 < 30; i++) a.push(i)
set.add
const s = new Set() for (let i = 0; i < 30; 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:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.push
10908222.0 Ops/sec
set.add
1910069.6 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark provided compares the performance of two different ways to add elements to a collection in JavaScript: using an array's `push` method and a Set's `add` method. The test specifically uses 30 elements to observe how each approach performs in terms of execution speed. ### Test Cases 1. **`array.push`:** - **Operation:** This test initializes an empty array, `a`, and then iteratively adds integers from 0 to 29 to it using the `push` method. - **Expected Outcome:** The array's length will be 30 after the loop execution, providing a straightforward way to accumulate items. 2. **`set.add`:** - **Operation:** This test initializes an empty Set, `s`, and similarly adds integers from 0 to 29 using the `add` method. - **Expected Outcome:** Like the array approach, the size of the set will be 30, but it automatically handles unique values. ### Performance Results The results of the latest benchmark indicate that `array.push` is significantly faster than `set.add`, with execution speeds as follows: - **Array Push:** 25,681,106 executions per second - **Set Add:** 4,162,686.75 executions per second ### Summary of Approaches **Pros and Cons:** 1. **Array with `push`:** - **Pros:** - Faster performance for adding elements due to lower overhead. - Simple and intuitive if the order of elements and duplicates are not a concern. - **Cons:** - Can lead to duplicate entries if not managed correctly (for example, when elements added multiple times). - Element retrieval or existence checks (e.g., `array.includes()`) can be slower as the size of the array grows, requiring full linear scans. 2. **Set with `add`:** - **Pros:** - Automatically manages uniqueness, ensuring no duplicate entries. - Better performance for checking if an element exists (`set.has()`), which operates in constant time on average due to the underlying hash structure. - **Cons:** - Slower for initial element addition compared to arrays in this specific benchmark scenario. - Limited in terms of directly accessing elements by index, as Sets maintain no order regarding element indexing. ### Other Considerations - **Alternatives:** - **Objects:** For scenarios requiring key-value pairs, JavaScript's objects may be considered. Performance would vary significantly based on use case (e.g., having different access patterns or data types). - **Map:** Similar to a Set, a Map allows key-value storage with uniqueness in keys and can be more appropriate when you need to associate data. - When you choose between these data structures, consider your specific use cases, including the need for performance versus the requirements for allowing duplicates, accessing elements by index, or managing unique items. By understanding the differences between using an array versus a Set in JavaScript, software engineers can make informed choices based on performance implications appropriate for their application needs.
Related benchmarks:
set.add vs array.push
Array of nulls
fill vs push
set.add vs array.push Fabien2
fill + map vs push
add vs object
Add item to array: push vs spread vs assign vs assign+grow
array.push() VS Set.add()
set.add vs array.push vc1
Comments
Confirm delete:
Do you really want to delete benchmark?