Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.add vs. array.push
(version: 0)
set.add vs. array.push
Comparing performance of:
Push vs Add
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Array.from({length: 100000}, () => Math.floor(Math.random() * 40)); var b = new Set(a);
Tests:
Push
a.push(191818);
Add
b.add(191818);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Push
Add
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Push
12228769.0 Ops/sec
Add
10020596.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition** The benchmark measures the performance difference between using `set.add` versus `array.push` in JavaScript. The script preparation code initializes two arrays of size 100,000 with random values between 0 and 39. It then creates a new Set object from the first array (`b = new Set(a)`). **Script Preparation Code** ```javascript var a = Array.from({length: 100000}, () => Math.floor(Math.random() * 40)); var b = new Set(a); ``` This code creates an array `a` with 100,000 elements and populates it with random values between 0 and 39. It then creates a new Set object `b` from the array. **Individual Test Cases** There are two test cases: 1. **Push**: Measures the performance of using `array.push(191818)` on the original array `a`. 2. **Add**: Measures the performance of using `set.add(191818)` on the Set object `b`. **Libraries and Special JS Features** * The `Array.from()` method is used to create a new array from an iterable (in this case, an object with a `length` property). * There are no libraries mentioned in the benchmark definition. * No special JavaScript features or syntax are used in this benchmark. Now, let's discuss the pros and cons of each approach: **Array.push()** Pros: * Fast and efficient when adding elements to the end of an array. * Suitable for large arrays where random access is required. Cons: * Can lead to memory fragmentation if elements are added at arbitrary indices. * May cause performance issues due to the need to update internal array metadata. **Set.add()** Pros: * Fast and efficient when adding unique elements to a set. * Ensures uniqueness of elements, which can be beneficial in certain use cases. Cons: * Can lead to slower performance for large sets or sets with many duplicate elements. * May not be suitable for use cases where random access is required. **Considerations** When choosing between `array.push()` and `set.add()`, consider the following factors: * **Performance**: If you need fast insertion of unique elements, `set.add()` might be a better choice. However, if you require random access to elements in the array or set, `array.push()` might be more suitable. * **Memory usage**: If memory is limited, `array.push()` might lead to increased memory fragmentation, while `set.add()` can help reduce memory usage by avoiding duplicate elements. **Alternatives** Other alternatives for adding elements to an array or set include: * Using `push()` with a new element, e.g., `a.push(newElement)`. * Using the spread operator (`...`) to create a new array from an existing one, e.g., `newArray = [...a, newElement]`. * Using `Array.prototype.concat()` to concatenate two arrays, e.g., `a = a.concat([newElement])`. Keep in mind that these alternatives may have different performance characteristics and memory usage compared to `array.push()` and `set.add()`.
Related benchmarks:
Array.from vs Spread. But with one thousand sequential elements and with one thousand random elements from zero and one thousand.
Array push vs
Array push or set
set.has vs. array.includes - large array - random Access
Comments
Confirm delete:
Do you really want to delete benchmark?