Skip to content

Instantly share code, notes, and snippets.

@aheckmann
Last active January 23, 2024 17:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aheckmann/6eefabd3c7a568451129 to your computer and use it in GitHub Desktop.
Save aheckmann/6eefabd3c7a568451129 to your computer and use it in GitHub Desktop.
var LRU = require('lru-cache');
var i = 0;
var sets = 0;
var start = Date.now();
var last = Date.now();
var max = 8193; // 8192 is the magic number upper perf limit of plain objects
var maxAge = 1000 * 60 * 60 * 24;
var cache = new LRU({
max: max,
maxAge: maxAge
});
function next() {
i++;
if ((i % 5000) === 0) {
var now = Date.now();
var elapsed = now - start;
console.log({
elapsed: elapsed + " ms",
duration: now - last,
iteration: i,
sets: sets
});
last = now;
}
var token = createToken(6);
var val = cache.get(token);
if (!val) {
sets++;
cache.set(token, createValue());
}
setImmediate(next);
}
var chars = 'abcdefg0987654321';
function createToken(len) {
var ret = '';
for (var i = 0; i < len; ++i) {
var index = Math.random() * chars.length | 0;
ret += chars[index];
}
return ret;
}
function createValue() {
return {
key: String(Math.random()),
something: ['portal', 'amazing'],
application: null
}
}
next();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment