const a = []
for (let i = 0; i < 30000; i++) a.push(i)
const s = new Set()
for (let i = 0; i < 30000; i++) s.add(i)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array.push | |
set.add |
Test name | Executions per second |
---|---|
array.push | 16084.0 Ops/sec |
set.add | 1292.0 Ops/sec |
The benchmark defined in the provided JSON compares the performance of two different data structure methods in JavaScript: array.push
and set.add
. Specifically, it measures how many times each method can be executed in one second.
array.push:
array.push
is significantly faster (16,084.02 executions per second).set.add:
Set
and adds 30,000 integers to this set.Set
automatically enforces uniqueness of its elements. If you try to add a duplicate, it will not be added, making it optimal for scenarios where unique values are required.set.add
is much slower (1,292.01 executions per second) compared to array.push
.The execution speed comparison clearly shows that, for this specific operation (adding a series of integers), the array.push
method outperforms set.add
by a considerable margin.
Map
could provide similar functionality while allowing efficient lookups.In situations where interactivity and data manipulation are more dynamic (like frequently changing datasets), choosing between arrays and sets would depend heavily on the specific requirements in terms of performance, memory consumption, and usability within the broader application context. Different operations (like searching, deleting, or iterating over data) will have varying performance implications based on the chosen structure.