{"ScriptPreparationCode":"\u0022use strict\u0022;\r\n\r\nlet Lru = function(cacheSize,callbackBackingStoreLoad,elementLifeTimeMs=1000){\r\n let me=this;\r\n let maxWait = elementLifeTimeMs;\r\n let size = parseInt(cacheSize,10);\r\n let mapping = {}; \r\n let buf = []; \r\n for(let i=0;i\u003Csize;i\u002B\u002B)\r\n {\r\n let rnd = Math.random();\r\n mapping[rnd] = i;\r\n buf.push({data:\u0022\u0022,visited:false, key:rnd, time:Date.now()});\r\n }\r\n let ctr= 0; \r\n let ctrEvict= parseInt(cacheSize/2,10);\r\n let loadData = callbackBackingStoreLoad; \r\n this.get = function(key){\r\n if(key in mapping)\r\n {\r\n \r\n \r\n if(Date.now() - buf[mapping[key]].time \u003E maxWait)\r\n {\r\n delete mapping[key];\r\n return me.get(key);\r\n }\r\n else\r\n {\r\n \r\n buf[mapping[key]].visited=true;\r\n buf[mapping[key]].time = Date.now();\r\n return buf[mapping[key]].data;\r\n }\r\n }\r\n else\r\n { \r\n \r\n\r\n \r\n let ctrFound = -1;\r\n while(ctrFound===-1)\r\n {\r\n \r\n if(buf[ctr].visited)\r\n {\r\n buf[ctr].visited=false;\r\n }\r\n ctr\u002B\u002B;\r\n if(ctr \u003E= size)\r\n {\r\n ctr=0;\r\n }\r\n\r\n \r\n if(!(buf[ctrEvict].visited))\r\n {\r\n \r\n ctrFound = ctrEvict;\r\n }\r\n\r\n ctrEvict\u002B\u002B;\r\n if(ctrEvict \u003E= size)\r\n {\r\n ctrEvict=0;\r\n }\r\n }\r\n delete mapping[buf[ctrFound].key];\r\n mapping[key] = ctrFound;\r\n let dataKey = loadData(key);\r\n buf[ctrFound] = {data:dataKey, visited:false, key:key,time:Date.now()};\r\n return buf[ctrFound].data;\r\n }\r\n };\r\n};\r\n\r\ndocument.lru = new Lru(100,function(key){ \r\n /* cache miss, load from data-store */ \r\n let wait=Date.now();let aa=0;\r\n while(Date.now()-wait\u003C1){aa\u002B\u002B;}\r\n return aa.toString()\u002Bkey;\r\n},5000 /*miliseconds before next get() invalidates data */);\r\n\r\ndocument.lru2 = new Lru(1000,function(key){ \r\n /* cache miss, load from data-store */ \r\n let wait=Date.now();let aa=0;\r\n while(Date.now()-wait\u003C1){aa\u002B\u002B;}\r\n return aa.toString()\u002Bkey;\r\n},5000 /*miliseconds before next get() invalidates data */);\r\n\r\ndocument.lru3 = new Lru(10000,function(key){ \r\n /* cache miss, load from data-store */ \r\n let wait=Date.now();let aa=0;\r\n while(Date.now()-wait\u003C1){aa\u002B\u002B;}\r\n return aa.toString()\u002Bkey;\r\n},5000 /*miliseconds before next get() invalidates data */);","TestCases":[{"Name":"Test1","Code":"let c=0;\r\nlet tt=Date.now();\r\nfunction runThis()\r\n{\r\n\r\n\tfor(let i=0;i\u003C10000;i\u002B\u002B)\r\n {\r\n \tlet myData = document.lru.get(parseInt(Math.random()*100,10).toString()\u002B\u0022ee\u0022);\r\n \tconsole.log(myData);\r\n }\r\n \r\n}\r\n\r\nrunThis();\r\n \r\n \r\n \r\n \r\n ","IsDeferred":false},{"Name":"Test2","Code":"let c=0;\r\nlet tt=Date.now();\r\nfunction runThis()\r\n{\r\n\r\n\tfor(let i=0;i\u003C10000;i\u002B\u002B)\r\n {\r\n \tlet myData = document.lru2.get(parseInt(Math.random()*100,10).toString()\u002B\u0022ee\u0022);\r\n \tconsole.log(myData);\r\n }\r\n \r\n}\r\n\r\nrunThis();\r\n ","IsDeferred":false},{"Name":"Test3","Code":"let c=0;\r\nlet tt=Date.now();\r\nfunction runThis()\r\n{\r\n\r\n\tfor(let i=0;i\u003C10000;i\u002B\u002B)\r\n {\r\n \tlet myData = document.lru3.get(parseInt(Math.random()*100,10).toString()\u002B\u0022ee\u0022);\r\n \tconsole.log(myData);\r\n }\r\n \r\n}\r\n\r\nrunThis();\r\n ","IsDeferred":false}]}