{"ScriptPreparationCode":"// \u5047\u7684 entityById \u8CC7\u6599\r\nwindow.entityById = {};\r\nfor (let i = 1; i \u003C= 200; i\u002B\u002B) {\r\n const price = (i * 10).toString(); // \u7522\u751F 10, 20, 30, ..., 1000 \u7684 price key\r\n const amount = Math.floor(Math.random() * 100) \u002B 1; // \u96A8\u6A5F\u7522\u751F 1 \u5230 100 \u7684 amount\r\n window.entityById[price] = amount;\r\n}\r\n\r\n// \u5047\u7684 data \u8CC7\u6599\r\nwindow.data = [];\r\nfor (let i = 0; i \u003C 20; i\u002B\u002B) {\r\n const price = ((Math.floor(Math.random() * 100) \u002B 1) * 10).toString(); // \u96A8\u6A5F\u9078\u64C7 10, 20, 30, ..., 1000 \u7684 price key\r\n const amount = Math.floor(Math.random() * 101); // \u96A8\u6A5F\u7522\u751F 0 \u5230 100 \u7684 amount\r\n window.data.push([price, amount]);\r\n}","TestCases":[{"Name":"Create light","Code":"function getEntityById(state, data) {\r\n const entityById = { ...state.entityById }\r\n data.forEach(([price, _amount]) =\u003E {\r\n const amount = Number(_amount)\r\n if (amount === 0) {\r\n delete entityById[price]\r\n } else {\r\n entityById[price] = { amount }\r\n }\r\n })\r\n return entityById\r\n}\r\n\r\naskEntityById = getEntityById(window.entityById, window.data)\r\n\r\nconst minAsks = Math.min(...Object.keys(askEntityById).map(Number))","IsDeferred":false},{"Name":"Directly find","Code":"function getMinAsks(state, data) {\r\n const sortedAsks = Object.keys(entityById).sort((a, b) =\u003E (Number(a) - Number(b) ? 1: -1))\r\n const idFromData = {}\r\n\r\n data.forEach(([price, _amount]) =\u003E {\r\n // array deconstructor is slow\r\n const amount = Number(_amount)\r\n if (amount === 0) {\r\n idFromData[price] = false\r\n } else {\r\n idFromData[price] = true\r\n }\r\n })\r\n\r\n const sortedIdFromData = Object.keys(idFromData).sort((a, b) =\u003E (Number(a) - Number(b) ? 1 : -1))\r\n\r\n let minAsk\r\n let minStateAsk = sortedAsks.shift()\r\n let minDataAsk = sortedIdFromData.shift()\r\n while (!minAsk \u0026\u0026 (sortedAsks.length \u003E 0 || sortedIdFromData.length \u003E 0)) {\r\n if (!minDataAsk) {\r\n if (idFromData[String(minStateAsk)]) {\r\n minAsk = Number(minStateAsk)\r\n } else {\r\n minStateAsk = sortedAsks.shift()\r\n }\r\n } else if (!minStateAsk) {\r\n if (idFromData[String(minDataAsk)]) {\r\n minAsk = Number(minDataAsk)\r\n } else {\r\n minDataAsk = sortedIdFromData.shift()\r\n }\r\n } else if (Number(minStateAsk) \u003C= Number(minDataAsk)) {\r\n if (idFromData[String(minStateAsk)]) {\r\n minAsk = Number(minStateAsk)\r\n } else {\r\n minStateAsk = sortedAsks.shift()\r\n }\r\n } else if (Number(minStateAsk) \u003E Number(minDataAsk)) {\r\n if (idFromData[String(minDataAsk)]) {\r\n minAsk = Number(minDataAsk)\r\n } else {\r\n minDataAsk = sortedIdFromData.shift()\r\n }\r\n }\r\n \r\n }\r\n\r\n return minAsk\r\n}\r\n\r\nconst minAsks = getMinAsks(window.entityById, window.data)","IsDeferred":false}]}