const map = new Map();
const obj = {};
function generateKeys(startIndex, endIndex) {
const res = [];
for (let i = startIndex; i < endIndex; i++) {
res.push(btoa(i));
}
return res;
}
const kvs = generateKeys(0, 1000);
const newKvs = generateKeys(10000, 12000);
kvs.forEach(kv => {
map.set(kv, kv);
obj[kv] = kv;
});
kvs.forEach(kv => {
map.get(kv);
});
kvs.forEach(kv => {
obj[kv];
});
kvs.forEach(kv => {
map.set(kv, kv);
});
kvs.forEach(kv => {
obj[kv] = kv;
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Map get | |
Obj get | |
Map set | |
Obj set |
Test name | Executions per second |
---|---|
Map get | 48445.0 Ops/sec |
Obj get | 8981.6 Ops/sec |
Map set | 6869.4 Ops/sec |
Obj set | 7148.2 Ops/sec |
The benchmark on MeasureThat.net compares the performance of JavaScript Maps versus JavaScript Objects when using strings as keys and values. The benchmarks measure the efficiency of "get" and "set" operations for both data structures.
Map get
: Retrieving values using the get()
method.Map set
: Storing values using the set()
method.Obj get
: Accessing properties directly through the bracket notation.Obj set
: Assigning properties directly using bracket notation.From the benchmark results provided, there are observable execution speeds for each operation (measured in executions per second):
Pros:
get
operation compared to objects, as seen in the results. This is particularly advantageous when frequent retrievals are required.Cons:
set
operation in this benchmark.Pros:
Cons:
In conclusion, this benchmark provides significant insights into the performance trade-offs between Maps and Objects in JavaScript for specific operations. Understanding these nuances enables software engineers to make optimized choices based on their application's needs.