{"ScriptPreparationCode":"const array = [];\r\n\r\nfor (let i = 0; i \u003C 2000; i\u002B\u002B) {\r\n array.push({\r\n key: i,\r\n prop: Math.floor(Math.random() * 10000)\r\n })\r\n}\r\n\r\nconst hashmap = array.reduce((mapper, next) =\u003E ({\r\n ...mapper,\r\n [next.key]: next\r\n}), {});\r\n\r\nfunction findInArray (key) {\r\n const index = bs(array, key, function(element, needle) {\r\n return element.key - needle;\r\n });\r\n return index \u003E 0 ? array[index] : undefined;\r\n}\r\n\r\nfunction findInHashMap (key) {\r\n return hashmap[key] || undefined;\r\n}\r\n\r\nfunction bs() {\r\n var mid, cmp;\r\n \r\n if(low === undefined)\r\n low = 0;\r\n \r\n else {\r\n low = low|0;\r\n if(low \u003C 0 || low \u003E= haystack.length)\r\n throw new RangeError(\u0022invalid lower bound\u0022);\r\n }\r\n \r\n if(high === undefined)\r\n high = haystack.length - 1;\r\n \r\n else {\r\n high = high|0;\r\n if(high \u003C low || high \u003E= haystack.length)\r\n throw new RangeError(\u0022invalid upper bound\u0022);\r\n }\r\n \r\n while(low \u003C= high) {\r\n // The naive \u0060low \u002B high \u003E\u003E\u003E 1\u0060 could fail for array lengths \u003E 2**31\r\n // because \u0060\u003E\u003E\u003E\u0060 converts its operands to int32. \u0060low \u002B (high - low \u003E\u003E\u003E 1)\u0060\r\n // works for array lengths \u003C= 2**32-1 which is also Javascript\u0027s max array\r\n // length.\r\n mid = low \u002B ((high - low) \u003E\u003E\u003E 1);\r\n cmp = \u002Bcomparator(haystack[mid], needle, mid, haystack);\r\n \r\n // Too low.\r\n if(cmp \u003C 0.0)\r\n low = mid \u002B 1;\r\n \r\n // Too high.\r\n else if(cmp \u003E 0.0)\r\n high = mid - 1;\r\n \r\n // Key found.\r\n else\r\n return mid;\r\n }\r\n \r\n // Key not found.\r\n return ~low;\r\n }","TestCases":[{"Name":"find by array","Code":"const target = Math.floor(Math.random * 2000);\r\nfindInArray(target);","IsDeferred":false},{"Name":"find by hashmap","Code":"const target = Math.floor(Math.random * 2000);\r\nfindInHashMap(target);","IsDeferred":false}]}