Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.add vs array.push
(version: 0)
Comparing performance of:
array.push vs set.add
Created:
6 years ago
by:
Registered User
Jump to the latest result
Tests:
array.push
const a = [] for (let i = 0; i < 3000; i++) a.push(i)
set.add
const s = new Set() for (let i = 0; i < 3000; 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:
2 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:148.0) Gecko/20100101 Firefox/148.0
Browser/OS:
Firefox 148 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.push
126995.2 Ops/sec
set.add
18409.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **What is being tested?** The provided benchmark tests two different ways to add elements to an array or a Set data structure in JavaScript: 1. `array.push`: This method adds one element to the end of an array. 2. `set.add`: This method adds one element to a Set data structure. These test cases aim to measure the performance difference between using `push` on arrays and `add` on Sets for large numbers of elements. **Options compared** The benchmark compares two approaches: * **Array.push**: This is the traditional way of adding elements to an array in JavaScript. When you call `a.push(i)`, it adds the element `i` to the end of the array. * **Set.add**: This method adds the element `i` to a Set data structure, which automatically removes any duplicates. **Pros and Cons** Here's a brief summary: * **Array.push**: + Pros: Simple, widely supported, and easy to understand. It works well for small to medium-sized arrays. + Cons: For large arrays or datasets, `push` can lead to slower performance due to the need to reallocate memory and create new arrays. * **Set.add**: + Pros: Faster performance for large datasets since Sets only store unique elements. This makes it suitable for tasks like data deduplication or filtering. + Cons: May not be suitable for small datasets where the overhead of creating a Set is unnecessary. **Library and purpose** In this benchmark, no external libraries are used beyond JavaScript's built-in `Set` and `Array` objects. No special JavaScript features or syntax are required to run these tests. The focus is on the fundamental differences between `push` on arrays and `add` on Sets. **Other alternatives** For sets, you might consider using other data structures like: * `Map`: A Map can be used as a set when keys are hashable and uniqueness is guaranteed. * `WeakSet`: A WeakSet is similar to a Set but doesn't prevent properties from being garbage collected. However, for simple use cases where performance matters, the built-in Set object is usually the best choice.
Related benchmarks:
set.add vs. array.push
Javascript: Spread vs push
Add a property to an array using Array.push vs spread (...) operator (fixed)
Spread vs Push when adding into array
Comments
Confirm delete:
Do you really want to delete benchmark?