const CHUNK_SIZE = 100000;
const cache = {};
for (let i = 0; i < CHUNK_SIZE; i++) {
if (i % 2 === 0) {
cache[i] = JSON.stringify({ TzId: i, Name: `Tz${i}`});
}
}
for (let i = 0; i < CHUNK_SIZE; i++) {
if (cache.hasOwnProperty(i)) {
console.log(cache[i]);
} else {
console.log('empty');
}
}
const CHUNK_SIZE = 100000;
const cache = new Map();
for (let i = 0; i < CHUNK_SIZE; i++) {
if (i % 2 === 0) {
cache.set(i, JSON.stringify({ TzId: i, Name: `Tz${i}`}));
}
}
for (let i = 0; i < CHUNK_SIZE; i++) {
if (cache.has(i)) {
console.log(cache.get(i));
} else {
console.log('empty');
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object cache | |
Map cache |
Test name | Executions per second |
---|---|
Object cache | 3.0 Ops/sec |
Map cache | 2.8 Ops/sec |
Let's dive into the benchmark and explain what's being tested.
The provided JSON represents two test cases, each measuring the performance of an in-memory caching mechanism in Node.js. The two mechanisms being compared are:
cache
) to store key-value pairs.Map
data structure to store key-value pairs.What's being tested?
In both cases, the benchmark creates an array of 100,000 elements and iterates over it, storing some values in the cache (every second element). The test then measures how quickly the cached values can be retrieved from the cache. Specifically, it checks if a value is present in the cache by using hasOwnProperty
or has()
methods for the object cache and map respectively.
Options Compared:
The two options are:
Map
data structure to store key-value pairs.Map
s are designed to be more memory-efficientMap
data structureLibrary and Purpose:
The JSON.stringify()
function is used to serialize objects into a string format, which can be stored in the cache. The purpose of this function is to ensure that the cached values can be properly stored and retrieved.
No special JavaScript features or syntax are required for these benchmarks.
Other Considerations:
Alternatives:
Other caching mechanisms that could be tested in a similar way include: