Last active
January 23, 2024 17:07
-
-
Save aheckmann/6eefabd3c7a568451129 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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