Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
map.set vs array.push
(version: 0)
Comparing performance of:
map.set vs array.push
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
map.set
const map = new Map(); for(let i=0; i<10000; i++) { map.set(i, i); }
array.push
const arr = []; for(let i=0; i<10000; i++) { arr.push(i); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
map.set
array.push
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
map.set
1632.0 Ops/sec
array.push
24542.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand the test being performed on MeasureThat.net. **What is being tested?** The provided benchmark tests two JavaScript operations: `Map.set` and `array.push`. The goal of this benchmark is to measure which operation is faster when adding 10,000 elements to a data structure. **Options compared** There are only two options being compared: 1. `map.set`: This method sets the value associated with a key in a Map object. 2. `array.push`: This method adds one or more elements to the end of an array. **Pros and Cons of each approach:** **`map.set`** Pros: * Efficient use of memory, as it only allocates space for the key-value pairs being added. * Fast lookups, as Maps have O(1) average time complexity for `set`, `get`, and `has` operations. Cons: * May incur additional overhead due to the creation of a new key-value pair, which can lead to slower performance compared to modifying an existing array. **`array.push`** Pros: * Can reuse existing memory, as it modifies the original array. * Fast append operation with O(1) average time complexity. Cons: * May require more memory allocation and deallocation, leading to slower performance. * Slower lookups due to linear search in an unsorted array. **Other considerations:** Both options have a linear relationship between the size of the input data and the execution time. However, `map.set` may be faster for small inputs due to its faster lookup times and more efficient memory usage. For larger inputs, `array.push` might dominate due to its better cache locality and reuse of existing memory. **Library used** In this benchmark, no libraries are explicitly mentioned. However, the use of `Map` suggests that the benchmark is targeting modern JavaScript engines that support the Map data structure. **Special JS feature or syntax** The benchmark does not appear to rely on any special JavaScript features or syntax beyond the standard methods and data structures (i.e., `Map`, arrays).
Related benchmarks:
.map() vs for-of + push
spread vs push 3
Map.forEach vs Array.forEach vs Array.from(Map.prototype.values()).forEach
new Map vs set array to map
Array Spread vs Fill vs New Array
Comments
Confirm delete:
Do you really want to delete benchmark?