Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.add vs array.push for massiver objects
(version: 0)
Comparing performance of:
array.push vs set.add
Created:
2 years ago
by:
Registered User
Jump to the latest result
Tests:
array.push
const massiveString = new Array(1048576).fill("s").join("") const myMassiveObject = new Array(1024).fill(1).reduce((p, v, i) => { p.i = massiveString; return p; }, {}); const a = [] for (let i = 0; i < 3000; i++) a.push({myMassiveObject, d: Date().now})
set.add
const massiveString = new Array(1048576).fill("s").join("") const myMassiveObject = new Array(1024).fill(1).reduce((p, v, i) => { p.i = massiveString; return p; }, {}); const s = new Set() for (let i = 0; i < 3000; i++) s.add({myMassiveObject, d: Date().now})
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark. **Benchmark Definition** The benchmark is designed to compare the performance of two approaches: using `Array.prototype.push()` and using `Set.prototype.add()`. The goal is to see which method is faster when adding a large number of objects (specifically, 3000 objects) with a complex property (a massive string object). **Options Compared** The benchmark compares two options: 1. **`array.push()`**: This approach creates an array and pushes a large number of objects onto it using the `push()` method. 2. **`set.add()`**: This approach uses a `Set` data structure to add a large number of objects, each with a complex property (the massive string object). **Pros and Cons** * **`array.push()`**: + Pros: easy to implement, widely supported, and well-known. + Cons: can be slower due to the overhead of creating and manipulating arrays, especially for large numbers of elements. * **`set.add()`**: + Pros: efficient use of memory, fast lookup and insertion times, and good cache locality. + Cons: requires understanding of `Set` data structure, might require additional setup (e.g., declaring the set variable), and not as widely supported or well-known. **Library/Features Used** * No libraries are explicitly mentioned in the benchmark definition. However, it's likely that the `Set` data structure is being used from the built-in JavaScript API. * No special JavaScript features or syntax are required for this benchmark (e.g., no ES6+ features, no Web Workers, etc.). **Other Considerations** * The benchmark uses a realistic scenario with a large number of elements and complex properties to test performance. * The use of `Date().now` as a property value adds some overhead due to the timestamp calculation, but it's unlikely to be significant compared to the overall performance difference between `push()` and `add()`. * The benchmark is run on a Chrome 110 browser, which might affect the results (e.g., different JavaScript engine optimizations or caching). **Alternative Benchmarks** If you'd like to explore alternative approaches or variations of this benchmark: 1. **Use a different data structure**: Instead of using `Set`, try using `Array` with `push()` or another data structure like `Map`. 2. **Increase the number of elements**: Scale up the number of objects being added to test performance at larger scales. 3. **Add more complexity**: Introduce additional properties, methods, or operations (e.g., sorting, filtering, or merging) within each object being added to increase the benchmark's realism. 4. **Test on different browsers or platforms**: Expand the benchmark to cover multiple browsers, operating systems, or architectures to see how performance varies across environments. Keep in mind that each change may alter the results and require adjustments to the benchmark setup accordingly.
Related benchmarks:
set.add vs array.push
spread vs push large
Javascript: Spread vs push
Adding to an array with push.apply vs spread
Spread vs Push when adding into array
Comments
Confirm delete:
Do you really want to delete benchmark?