Last active
October 16, 2025 14:05
-
-
Save ForbesLindesay/9088eca0459965078003828f2439011d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const LOOPS = 10 | |
| const [tiny, small, medium, large, huge] = [1e1, 1e2, 1e4, 1e6, 1e7] | |
| const configs = [ | |
| { size: tiny, tests: huge }, | |
| { size: small, tests: large }, | |
| { size: medium, tests: medium }, | |
| { size: large, tests: small }, | |
| ] | |
| for (const { size, tests } of configs) { | |
| const arr = Array.from({ length: size }, () => crypto.randomUUID()) | |
| const map = new Map(arr.map(k => [k, true])) | |
| const set = new Set(arr) | |
| const obj = Object.fromEntries(arr.map(k => [k, true])) | |
| const valsToTest = Array.from({ length: tests }, () => | |
| Math.random() < 0.5 ? arr[Math.floor(Math.random() * size)] : crypto.randomUUID() | |
| ) | |
| const title = `dataset size: ${size.toLocaleString()}; number of tests: ${tests.toLocaleString()}; number of loops: ${LOOPS.toLocaleString()}` | |
| console.log(`\n-> ${title}`) | |
| for (const [target, method] of [ | |
| [arr, (a, v) => a.indexOf(v) !== -1], | |
| [arr, (a, v) => a.includes(v)], | |
| [map, (a, v) => a.has(v)], | |
| [set, (a, v) => a.has(v)], | |
| [obj, (a, v) => Object.prototype.hasOwnProperty.call(a, v)], | |
| ]) { | |
| const subtitle = method | |
| .toString() | |
| .substring(`(a, v) => `.length) | |
| .replace(/\ba\b/g, target.constructor.name) | |
| console.time(subtitle) | |
| for (let i = 0; i < LOOPS; i++) { | |
| let countTrue = 0 | |
| for (const val of valsToTest) { | |
| if (method(target, val)) { | |
| countTrue++ | |
| } | |
| } | |
| const ratio = countTrue / tests | |
| if (ratio > 0.6 || ratio < 0.4) { | |
| throw new Error(`unbalanced ratio, maybe bad input data: ${ratio}`) | |
| } | |
| } | |
| console.timeEnd(subtitle) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -> dataset size: 10; number of tests: 10,000,000; number of loops: 10 | |
| Array.indexOf(v) !== -1: 14.305s | |
| Array.includes(v): 13.774s | |
| Map.has(v): 3.361s | |
| Set.has(v): 3.106s | |
| Object.prototype.hasOwnProperty.call(Object, v): 6.207s | |
| -> dataset size: 100; number of tests: 1,000,000; number of loops: 10 | |
| Array.indexOf(v) !== -1: 8.331s | |
| Array.includes(v): 8.087s | |
| Map.has(v): 273.136ms | |
| Set.has(v): 260.883ms | |
| Object.prototype.hasOwnProperty.call(Object, v): 546.796ms | |
| -> dataset size: 10,000; number of tests: 10,000; number of loops: 10 | |
| Array.indexOf(v) !== -1: 12.599s | |
| Array.includes(v): 12.631s | |
| Map.has(v): 3.91ms | |
| Set.has(v): 3.498ms | |
| Object.prototype.hasOwnProperty.call(Object, v): 3.958ms | |
| -> dataset size: 1,000,000; number of tests: 100; number of loops: 10 | |
| Array.indexOf(v) !== -1: 13.057s | |
| Array.includes(v): 12.958s | |
| Map.has(v): 0.192ms | |
| Set.has(v): 0.14ms | |
| Object.prototype.hasOwnProperty.call(Object, v): 0.122ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment