{"ScriptPreparationCode":"function createNode(key, value) {\r\n return {\r\n newer: null,\r\n older: null,\r\n key: key,\r\n value: value\r\n };\r\n}\r\n\r\nfunction LRU(size) {\r\n this.size = size;\r\n\r\n this.length = 0;\r\n this.head = null;\r\n this.last = this.head;\r\n // Require map polyfill\r\n this.map = new Map();\r\n}\r\n\r\nLRU.prototype.has = function(key) {\r\n return this.map.has(key);\r\n};\r\n\r\nLRU.prototype._hoistNode = function(node) {\r\n if (this.head === node) {\r\n return;\r\n }\r\n\r\n if (this.last === node) {\r\n this.last = node.newer || node;\r\n this.last.older = null;\r\n }\r\n\r\n if (node.older) {\r\n node.older.newer = node.newer;\r\n }\r\n if (node.newer) {\r\n node.newer.older = node.older;\r\n }\r\n\r\n node.older = this.head;\r\n node.newer = null;\r\n\r\n if (this.head) {\r\n this.head.newer = node;\r\n }\r\n\r\n if (!this.last) {\r\n this.last = node;\r\n }\r\n this.head = node;\r\n};\r\n\r\nLRU.prototype.get = function(key) {\r\n if (!this.has(key)) {\r\n return undefined;\r\n }\r\n\r\n var node = this.map.get(key);\r\n\r\n this._hoistNode(node);\r\n\r\n return node.value;\r\n};\r\n\r\nLRU.prototype.set = function(key, value) {\r\n var node;\r\n if (this.has(key)) {\r\n node = this.map.get(key);\r\n node.value = value;\r\n } else if (this.length \u003C this.size) {\r\n node = createNode(key, value);\r\n this.map.set(key, node);\r\n this.length \u002B= 1;\r\n } else {\r\n node = this.last;\r\n this.map.delete(node.key);\r\n node.value = value;\r\n node.key = key;\r\n this.map.set(key, node);\r\n }\r\n this._hoistNode(node);\r\n};\r\n\r\nfunction memoizeWithLRU(size, prop, fn) {\r\n var cache = new LRU(size);\r\n\r\n return function (value) {\r\n var key = prop.call(null, value);\r\n if (cache.has(key)) {\r\n return cache.get(key);\r\n }\r\n\r\n const c = fn.call(this, value);\r\n cache.set(key, c);\r\n return c;\r\n }\r\n}\r\n","TestCases":[{"Name":"LRUMemoizeWith","Code":"var memo = memoizeWithLRU(50,i =\u003E i, i =\u003E i * 2);\r\n\r\nfor (let i = 0; i \u003C 5000; \u002B\u002Bi) {\r\n memo(i);\r\n}\r\nfor (let i = 0; i \u003C 5000; \u002B\u002Bi) {\r\n memo(i);\r\n}","IsDeferred":false},{"Name":"Ramda memoizeWith","Code":"var memo = R.memoizeWith(i =\u003E i, i =\u003E i * 2);\r\n\r\nfor (let i = 0;i\u003C 5000; \u002B\u002Bi ) memo(i);\r\nfor (let i = 0;i\u003C 5000; \u002B\u002Bi ) memo(i);","IsDeferred":false}]}