const set = new Set();
for (let i = 0; i < 1000; ++i) {
set.add(`key_${i}`);
}
let result = 0;
for (let j = 0; j < 100; ++j) {
const clone = new Set(set);
for (let k = -500; k < 500; ++k) {
result += clone.has(`key_${k}`);
}
}
console.log(result);
const obj = {};
for (let i = 0; i < 1000; ++i) {
obj[`key_${i}`] = true;
}
let result = 0;
for (let j = 0; j < 100; ++j) {
const clone = Object.assign({}, obj);
for (let k = -500; k < 500; ++k) {
result += clone.hasOwnProperty(`key_${k}`);
}
}
console.log(result);
const map = new Map();
for (let i = 0; i < 1000; ++i) {
map.set(`key_${i}`, true);
}
let result = 0;
for (let j = 0; j < 100; ++j) {
const clone = new Map(map);
for (let k = -500; k < 500; ++k) {
result += clone.get(`key_${k}`);
}
}
console.log(result);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Set | |
Object | |
Map |
Test name | Executions per second |
---|---|
Set | 190.2 Ops/sec |
Object | 29.2 Ops/sec |
Map | 90.1 Ops/sec |
The benchmark you're analyzing evaluates the performance of three different data structures in JavaScript: Set
, Object
, and Map
. These data structures are commonly used for storing key-value pairs, each with its unique characteristics, strengths, and weaknesses, particularly when it comes to performance in operations like adding, searching, and cloning.
The benchmark consists of three test cases, each using one of the three data structures. In each case, 1,000 keys are added, and then a cloned version of the data structure is created. Subsequently, the benchmark checks for the presence of keys in the cloned structure across a range of -500
to 499
. The performance is measured in terms of "Executions Per Second".
Set
const clone = new Set(set);
clone.has(key)
Set
for membership checks avoids duplicates, ensuring that only unique entries are present.Object
const clone = Object.assign({}, obj);
clone.hasOwnProperty(key)
Object.assign()
creates a shallow copy of the object.Set
and Map
, particularly when hasOwnProperty
is used, which may have performance implications when checking for the existence of a lot of keys.hasOwnProperty
.Map
const clone = new Map(map);
clone.get(key)
get
are generally fast and straightforward, especially when dealing with large datasets.Set
and Object
, which could be significant depending on the size of the dataset.Based on the benchmark results recorded:
Set
for this specific benchmark.hasOwnProperty
method and the potential pitfalls of working with prototype inheritance contribute to its lower efficiency, especially in operations involving multiple keys.Several alternatives or variations can be considered when choosing between these data structures:
Map
or Set
is generally more advisable than an Object due to their optimized membership checks.Map
is preferred due to its ability to retain complex structures as keys.Set
is preferable because of its minimal overhead and efficient membership validation.In conclusion, the choice between Set
, Object
, and Map
should take into account the specific requirements of the application, especially concerning data complexity, performance needs, and memory constraints. The benchmarks provide valuable insight into how various data structures perform under specific conditions, guiding developers in their selection process.