Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Binary Search vs Array.prototype.indexOf
I'm just wondering if it is optimized under the hood.
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0
Browser:
Firefox 140
Operating system:
Windows
Device Platform:
Desktop
Date tested:
10 months ago
Test name
Executions per second
indexOf 1000
61780.8 Ops/sec
indexOf 100000
62.5 Ops/sec
indexOf 1000000
6.2 Ops/sec
binSearch 1000
6537.3 Ops/sec
binSearch 100000
60.8 Ops/sec
binSearch 1000000
6.2 Ops/sec
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
function createLargeList(n) { const arr = []; for (i=0;i<n;i++) { arr.push(i); } return arr; } function indexOf(arr, v) { return arr.indexOf(v); } function binSearch(arr, v) { if (arr.length === 0) return -1; let left = 0; let right = arr.length - 1; while (left <= right) { let mid = Math.floor((left + right) / 2); if (arr[mid] === v) { return mid; } else if (v < arr[mid]) { right = mid - 1; } else { left = mid + 1; } } return -1; }
Tests:
indexOf 1000
const l = 1000; const arr = createLargeList(l); const r = Math.floor(Math.random() * l); const x = indexOf(arr, r);
indexOf 100000
const l = 100000; const arr = createLargeList(l); const r = Math.floor(Math.random() * l); const x = indexOf(arr, r);
indexOf 1000000
const l = 1000000; const arr = createLargeList(l); const r = Math.floor(Math.random() * l); const x = indexOf(arr, r);
binSearch 1000
const l = 1000; const arr = createLargeList(l); const r = Math.floor(Math.random() * l); const x = binSearch(arr, r);
binSearch 100000
const l = 100000; const arr = createLargeList(l); const r = Math.floor(Math.random() * l); const x = binSearch(arr, r);
binSearch 1000000
const l = 1000000; const arr = createLargeList(l); const r = Math.floor(Math.random() * l); const x = binSearch(arr, r);