{"ScriptPreparationCode":"\r\n\r\nvar haystack = new Array(1000).fill().map((nop, i) =\u003E i);\r\nvar needles = new Array(500).fill().map(() =\u003E Math.floor(Math.random() * 1000));\r\n\r\n\r\nfunction equalSearch(values, needle, params = {}) {\r\n // Retrieve last index\r\n var last = values.length - 1;\r\n\r\n // Init params\r\n var {\r\n compare,\r\n from: start = 0,\r\n to: end = last,\r\n } = params;\r\n\r\n // Outbounds ?\r\n if (start \u003C 0 || end \u003E last) {\r\n return -1;\r\n }\r\n\r\n // No compare callback ?\r\n if (!compare) {\r\n // Set default compare function\r\n compare = typeof needle === \u0027string\u0027 || needle instanceof String\r\n ? (a, b) =\u003E a.localeCompare(b)\r\n : (a, b) =\u003E a - b;\r\n }\r\n\r\n // Do search ...\r\n while (start \u003C= end) {\r\n // Get the middle\r\n var middle = Math.floor((start \u002B end) / 2);\r\n\r\n // Compare values\r\n var result = compare(needle, values[middle]);\r\n\r\n // Find value ?\r\n if (!result) {\r\n return middle;\r\n }\r\n\r\n // Lower ?\r\n if (result \u003C 0) {\r\n end = middle - 1;\r\n\r\n // ... greather\r\n } else {\r\n start = middle \u002B 1;\r\n }\r\n }\r\n\r\n return -1;\r\n}\r\n","TestCases":[{"Name":"IndexOf","Code":"needles.forEach(needle =\u003E haystack.indexOf(needle));","IsDeferred":false},{"Name":"findIndex","Code":"needles.forEach(needle =\u003E haystack.findIndex(item =\u003E item === needle));","IsDeferred":false}]}