Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.push() VS Set.add()
(version: 1)
Comparing performance of:
array.push() vs Set.add()
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const numbers = Array(10).fill(Array.from({ length: 100000 }, (_, i) => i + 1)).flat();
Tests:
array.push()
const a = []; for (const number of numbers) { a.push(number); }
Set.add()
const s = new Set(); for (const number of numbers) { s.add(number); }
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:
4 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.push()
183.9 Ops/sec
Set.add()
38.3 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
In this benchmark, we are comparing two different methods for populating a collection in JavaScript: using the `Array.prototype.push()` method on an array versus using the `Set.prototype.add()` method to populate a Set. ### Benchmark Preparation **Data Preparation**: The benchmark preparation code initializes an array called `numbers`, which contains numbers from 1 to 100,000. This data structure serves as the input for both test cases. ### Comparisons 1. **`array.push()`** - **Definition**: The `push()` method adds one or more elements to the end of an array and returns the new length of the array. - **Example**: The benchmark case creates an empty array `a`, then iterates through the `numbers` array using a for-of loop, pushing each number into `a`. - **Pros**: - Arrays are versatile and supported in many cases within JavaScript. - The order of elements is maintained, and they allow duplicate entries. - Simplicity and familiarity make this approach straightforward for many developers. - **Cons**: - The performance of array manipulations can degrade with rapid growth (though in this case, we are only using push). - Arrays come with some overhead since they are designed to represent ordered collections. 2. **`Set.add()`** - **Definition**: The `add()` method adds a new element to a Set object, which can hold only unique values. - **Example**: The benchmark case initializes an empty Set `s` and iterates through the `numbers` array, using `add()` to include each number. - **Pros**: - Sets automatically handle uniqueness, preventing duplicates without explicit checks. - They are generally optimized for membership checks (i.e., to test if an element exists). - **Cons**: - Sets do not maintain the order of elements like arrays do. - They may introduce additional overhead due to the need to handle uniqueness, which can impact performance in specific scenarios. ### Performance Results According to the benchmark results: - The `array.push()` operation executed approximately 94.65 times per second. - The `Set.add()` operation executed around 52.30 times per second. From this data, we observe that `array.push()` outperforms `Set.add()` in this particular case. This is likely due to the simplicity of appending elements to an array compared to looking up and inserting into a Set, which is inherently more complex because it checks for uniqueness. ### Other Considerations - **Use Case**: The choice between an array and a Set should be informed by the specific requirements of the application. If order and potential duplicates are important, prefer arrays. If uniqueness is paramount, then Sets are preferable. - **Alternatives**: Alternative data structures or libraries might be chosen based on particular needs: - **Maps**: If key-value storage is needed with guaranteed unique keys. - **WeakSet or WeakMap**: For managing object references without preventing garbage collection. - **Typed Arrays**: For numerical data requiring high performance. In conclusion, the benchmark examines the efficiency of two collection types in handling data insertion, highlighting their unique advantages and disadvantages while presenting performance metrics that inform implementation decisions.
Related benchmarks:
Array initialization: preallocate vs push
set.add vs array.push
Array of nulls
fill vs for loop
Storing array length before for
array.fill VS array.push
push vs index123
Pushing vs Filling
Array.from vs Array.fill (zeroes)
Comments
Confirm delete:
Do you really want to delete benchmark?