{"ScriptPreparationCode":null,"TestCases":[{"Name":"Test1","Code":"\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\nlet lru = new Lru(9,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\nlet c=0;\r\nlet tt=Date.now();\r\nfunction runThis()\r\n{\r\n\r\n\r\n \r\n c\u002B\u002B;\r\nlet myData = lru.get(parseInt(Math.random()*10,10).toString()\u002B\u0022ee\u0022);\r\n\r\n \r\nif(c\u003C1000)\r\n runThis();\r\n else\r\n console.log(Date.now()-tt);\r\n}\r\n\r\nrunThis();\r\n \r\n \r\n \r\n \r\n ","IsDeferred":false},{"Name":"Test2","Code":"async function runThis()\r\n{\r\nfor(let i=0;i\u003C1000;i\u002B\u002B)\r\n{\r\n let wait=Date.now();let aa=0;\r\n while(Date.now()-wait\u003C1){aa\u002B\u002B;}\r\n console.log(parseInt(Math.random()*10,10));\r\n}\r\n}\r\n\r\nrunThis();","IsDeferred":false}]}