Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Array vs Object vs Set add item speed in 50000 iters 2
(version: 0)
Comparing performance of:
Map vs Array vs Object vs Set
Created:
4 years ago
by:
Registered User
Jump to the latest result
Tests:
Map
let randomU32 = function() { return Math.random() * (1 << 31) >>> 0; } let map = new Map(); for (let i = 0; i <= 50000; i++) { map.set(randomU32(), true); }
Array
let randomU32 = function() { return Math.random() * (1 << 31) >>> 0; } let arr = []; for (let i = 0; i <= 50000; i++) { arr[randomU32()] = true; }
Object
let randomU32 = function() { return Math.random() * (1 << 31) >>> 0; } let obj = {}; for (let i = 0; i <= 50000; i++) { obj[randomU32()] = true; }
Set
let randomU32 = function() { return Math.random() * (1 << 31) >>> 0; } let set = new Set(); for (let i = 0; i <= 50000; i++) { set.add(randomU32()); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Map
Array
Object
Set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map
358.1 Ops/sec
Array
197.8 Ops/sec
Object
205.7 Ops/sec
Set
384.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of different data structures in JavaScript is crucial to understand their strengths and weaknesses. **Data Structures Comparison** The benchmark compares the performance of four popular data structures: 1. **Map**: A Map (or dictionary) is an object that stores key-value pairs. In this case, each iteration adds a random key-value pair to the Map. 2. **Array**: An Array is a collection of elements that can be accessed by their index. Here, we're adding a value to an array using a random index. 3. **Object**: An Object (similar to a dictionary) stores key-value pairs but does not maintain any particular order. We're creating an object and adding key-value pairs to it. 4. **Set**: A Set is a collection of unique values, similar to a Map but without keys. In this case, we're adding random values to the Set. **Options Comparison** Here's what each option offers: * **Fastest execution time**: The data structure with the fastest execution time will likely be preferred in scenarios where speed is critical. * **Memory efficiency**: Data structures that use less memory can be beneficial for applications with limited resources. * **Cache performance**: The way elements are stored and accessed within a data structure affects cache performance, which impacts overall performance. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: ### Map * Pros: * Fast insertions (average O(1) time complexity) * Good cache locality due to its object-based nature * Cons: * Less memory-efficient than other data structures for storing large amounts of unique elements * May not be the best choice for very fast access times, as lookups can take O(1) time ### Array * Pros: * Good cache locality due to its contiguous array structure * Can be used with efficient indexing methods like binary search * Cons: * Less efficient than Maps or Sets when it comes to insertion (average O(n) time complexity) * May not provide the same level of uniqueness as Sets ### Object * Pros: * Good cache locality due to its object-based structure * Can be used with efficient lookup methods like property access * Cons: * Less memory-efficient than other data structures, especially when it comes to storing large amounts of unique elements * May not provide the same level of uniqueness as Sets ### Set * Pros: + Fast insertions (average O(1) time complexity) + Good cache locality due to its set-based structure - Does not maintain any particular order Cons: * Less memory-efficient than other data structures, especially when it comes to storing large amounts of unique elements * May not be the best choice for very fast access times, as lookups can take O(1) time **Library Usage** The benchmark uses several libraries that play a crucial role in its performance: * **Math.random()**: Generates random numbers. **Special JS Features/Syntax** None mentioned in the provided test cases.
Related benchmarks:
iterating from a filled object VS iterating from a map
new Map vs Array.from vs spread operator
new Map vs set array to map
Array Spread vs Fill vs New Array
Comments
Confirm delete:
Do you really want to delete benchmark?