let randomU32 = function() {
return Math.random() * (1 << 31) >>> 0;
}
let map = new Map();
for (let i = 0; i <= 50000; i++) {
map.set(randomU32(), true);
}
let randomU32 = function() {
return Math.random() * (1 << 31) >>> 0;
}
let arr = [];
for (let i = 0; i <= 50000; i++) {
arr[randomU32()] = true;
}
let randomU32 = function() {
return Math.random() * (1 << 31) >>> 0;
}
let obj = {};
for (let i = 0; i <= 50000; i++) {
obj[randomU32()] = true;
}
let randomU32 = function() {
return Math.random() * (1 << 31) >>> 0;
}
let set = new Set();
for (let i = 0; i <= 50000; i++) {
set.add(randomU32());
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Map | |
Array | |
Object | |
Set |
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 |
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:
Options Comparison
Here's what each option offers:
Pros and Cons
Here's a brief summary of the pros and cons of each approach:
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:
Special JS Features/Syntax
None mentioned in the provided test cases.