Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
LRU vs memoizeWith
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Mobile Safari/537.36
Browser:
Chrome Mobile 135
Operating system:
Android
Device Platform:
Mobile
Date tested:
one year ago
Test name
Executions per second
LRUMemoizeWith
676.6 Ops/sec
Ramda memoizeWith
1963.6 Ops/sec
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js" integrity="sha512-rZHvUXcc1zWKsxm7rJ8lVQuIr1oOmm7cShlvpV0gWf0RvbcJN6x96al/Rp2L2BI4a4ZkT2/YfVe/8YvB2UHzQw==" crossorigin="anonymous"></script>
Script Preparation code:
function createNode(key, value) { return { newer: null, older: null, key: key, value: value }; } function LRU(size) { this.size = size; this.length = 0; this.head = null; this.last = this.head; // Require map polyfill this.map = new Map(); } LRU.prototype.has = function(key) { return this.map.has(key); }; LRU.prototype._hoistNode = function(node) { if (this.head === node) { return; } if (this.last === node) { this.last = node.newer || node; this.last.older = null; } if (node.older) { node.older.newer = node.newer; } if (node.newer) { node.newer.older = node.older; } node.older = this.head; node.newer = null; if (this.head) { this.head.newer = node; } if (!this.last) { this.last = node; } this.head = node; }; LRU.prototype.get = function(key) { if (!this.has(key)) { return undefined; } var node = this.map.get(key); this._hoistNode(node); return node.value; }; LRU.prototype.set = function(key, value) { var node; if (this.has(key)) { node = this.map.get(key); node.value = value; } else if (this.length < this.size) { node = createNode(key, value); this.map.set(key, node); this.length += 1; } else { node = this.last; this.map.delete(node.key); node.value = value; node.key = key; this.map.set(key, node); } this._hoistNode(node); }; function memoizeWithLRU(size, prop, fn) { var cache = new LRU(size); return function (value) { var key = prop.call(null, value); if (cache.has(key)) { return cache.get(key); } const c = fn.call(this, value); cache.set(key, c); return c; } }
Tests:
LRUMemoizeWith
var memo = memoizeWithLRU(50,i => i, i => i * 2); for (let i = 0; i < 5000; ++i) { memo(i); } for (let i = 0; i < 5000; ++i) { memo(i); }
Ramda memoizeWith
var memo = R.memoizeWith(i => i, i => i * 2); for (let i = 0;i< 5000; ++i ) memo(i); for (let i = 0;i< 5000; ++i ) memo(i);