Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
NodeJs in-memory-cache mechanism
(version: 0)
Comparing performance of:
Object cache vs Map cache
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Object cache
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'); } }
Map cache
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'); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object cache
Map cache
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object cache
3.0 Ops/sec
Map cache
2.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. **Object Cache**: This uses a plain JavaScript object (`cache`) to store key-value pairs. 2. **Map Cache**: This uses a JavaScript `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: * **Object Cache**: uses a plain JavaScript object to store key-value pairs. + Pros: - Simple implementation - Fast lookups (O(1) average case) + Cons: - Limited size, as the cache is stored in memory - No built-in support for keys that require special handling * **Map Cache**: uses a JavaScript `Map` data structure to store key-value pairs. + Pros: - More flexible than objects (e.g., can handle keys with complex types) - Larger cache size, as `Map`s are designed to be more memory-efficient + Cons: - More complex implementation - May have slower lookups due to the overhead of the `Map` data structure **Library 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:** * **Cache size**: Both test cases create large caches (100,000 elements), which may impact performance. * **JavaScript engine optimization**: The results may vary depending on the specific JavaScript engine used by the browser. * **Device and platform variations**: The benchmark results may differ between different devices, platforms, and browsers. **Alternatives:** Other caching mechanisms that could be tested in a similar way include: * Using a library like LRU Cache or Memcached * Implementing a custom caching mechanism using a different data structure (e.g., a binary search tree) * Testing the performance of caching mechanisms in other programming languages, such as Python or Java
Related benchmarks:
_.uniq() vs Set() over large array
Perf difference when using looping over collection vs creating a cached projection for that access pattern (small array)
Perf difference when using looping over collection vs creating a cached projection for that access pattern (medium array)
Perf difference when using looping over collection vs creating a cached projection for that access pattern (large array)
Set string vs number (100k)
Comments
Confirm delete:
Do you really want to delete benchmark?