Last active
February 21, 2022 11:42
-
-
Save usualoma/38e4e32bf4c5b7a6e3628dbb7e55f507 to your computer and use it in GitHub Desktop.
This file contains 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
import Benchmark from 'benchmark' | |
const suite = new Benchmark.Suite() | |
const re = new RegExp([...Array(1000)].map((_, i) => `/${i}/(.*)()$`).join('|')) | |
const m = '/500/'.match(re) | |
console.log(m.length) | |
console.log(m.indexOf('')) | |
const io = Array.prototype.indexOf | |
const lio = Array.prototype.lastIndexOf | |
suite | |
.add('indexOf()', () => { | |
const i = m.indexOf('', 1) | |
return m.length - 1 === i || m[i + 1] !== '' ? i : i + 1 | |
}) | |
.add('lastIndexOf()', () => { | |
return m.lastIndexOf('') | |
}) | |
.add('lastIndexOf by for', () => { | |
for (let i = m.length - 1; i > 0; i--) { | |
if (m[i] === '') { | |
return i | |
} | |
} | |
return -1 | |
}) | |
.add('lastIndexOf by while', () => { | |
let i = m.length | |
while (i--) { | |
if (m[i] === '') { | |
return i | |
} | |
} | |
return -1 | |
}) | |
.add('Array.prototype.indexOf.call', () => { | |
io.call(m, '', 1) | |
}) | |
.add('Array.prototype.lastIndexOf.call', () => { | |
lio.call(m, '') | |
}) | |
.on('cycle', (event) => { | |
console.log(String(event.target)) | |
}) | |
.on('complete', function () { | |
console.log(`Fastest is ${this.filter('fastest').map('name')}`) | |
}) | |
.run({ async: true }) |
This file contains 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
indexOf() x 850,664 ops/sec ±0.37% (95 runs sampled) | |
lastIndexOf() x 319,252 ops/sec ±0.08% (98 runs sampled) | |
lastIndexOf by for x 857,151 ops/sec ±0.02% (94 runs sampled) | |
lastIndexOf by while x 687,093 ops/sec ±0.03% (99 runs sampled) | |
Array.prototype.indexOf.call x 851,614 ops/sec ±0.19% (99 runs sampled) | |
Array.prototype.lastIndexOf.call x 319,220 ops/sec ±0.17% (99 runs sampled) | |
Fastest is lastIndexOf by for |
This file contains 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
indexOf() x 1,670,325 ops/sec ±0.38% (98 runs sampled) | |
lastIndexOf() x 293,537 ops/sec ±0.11% (95 runs sampled) | |
lastIndexOf by for x 857,078 ops/sec ±0.02% (100 runs sampled) | |
lastIndexOf by while x 686,762 ops/sec ±0.04% (99 runs sampled) | |
Array.prototype.indexOf.call x 1,661,371 ops/sec ±0.27% (96 runs sampled) | |
Array.prototype.lastIndexOf.call x 293,507 ops/sec ±0.20% (96 runs sampled) | |
Fastest is indexOf() |
This file contains 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
indexOf() x 560,852 ops/sec ±0.77% (93 runs sampled) | |
lastIndexOf() x 328,680 ops/sec ±1.00% (90 runs sampled) | |
lastIndexOf by for x 958,723 ops/sec ±0.98% (92 runs sampled) | |
lastIndexOf by while x 757,040 ops/sec ±1.18% (89 runs sampled) | |
Array.prototype.indexOf.call x 517,647 ops/sec ±0.99% (88 runs sampled) | |
Array.prototype.lastIndexOf.call x 328,206 ops/sec ±0.62% (91 runs sampled) | |
Fastest is lastIndexOf by for |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment