jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
Testing a JS LRU vs an Object
<script>
function randomStr(len) {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz",schars="ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var str = '';
for (var i=0; i<len; i++) {
if(i==0)
{
var rnum = Math.floor(Math.random() * schars.length);
str += schars.substring(rnum,rnum+1);
}
else
{
var rnum = Math.floor(Math.random() * chars.length);
str += chars.substring(rnum,rnum+1);
}
}
return str;
}
class LRU {
constructor(max = 10) {
this.max = max;
this.cache = new Map();
}
get(key) {
let item = this.cache.get(key);
if (item) {
// refresh key
this.cache.delete(key);
this.cache.set(key, item);
}
return item;
}
set(key, val) {
// refresh key
if (this.cache.has(key)) this.cache.delete(key);
// evict oldest
else if (this.cache.size == this.max) this.cache.delete(this.first());
this.cache.set(key, val);
}
first() {
return this.cache.keys().next().value;
}
}
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
Object |
| ready |
LRU |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.