Last active
February 21, 2022 11:42
-
-
Save usualoma/70a4a75a8d09d2748853fa8915e3d024 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
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()', () => { | |
return m.indexOf('', 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 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
indexOf() x 525,422,544 ops/sec ±0.50% (92 runs sampled) | |
lastIndexOf() x 620,680 ops/sec ±0.05% (95 runs sampled) | |
lastIndexOf by for x 1,698,597 ops/sec ±0.03% (94 runs sampled) | |
lastIndexOf by while x 1,362,382 ops/sec ±0.19% (99 runs sampled) | |
Array.prototype.indexOf.call x 1,672,314 ops/sec ±0.02% (94 runs sampled) | |
Array.prototype.lastIndexOf.call x 620,908 ops/sec ±0.03% (98 runs sampled) | |
Fastest is indexOf() |
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
indexOf() x 611,477,018 ops/sec ±0.45% (91 runs sampled) | |
lastIndexOf() x 569,973 ops/sec ±0.10% (93 runs sampled) | |
lastIndexOf by for x 1,692,906 ops/sec ±0.03% (95 runs sampled) | |
lastIndexOf by while x 1,361,383 ops/sec ±0.08% (98 runs sampled) | |
Array.prototype.indexOf.call x 3,195,108 ops/sec ±0.06% (98 runs sampled) | |
Array.prototype.lastIndexOf.call x 570,631 ops/sec ±0.02% (98 runs sampled) | |
Fastest is indexOf() |
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
indexOf() x 517,641,986 ops/sec ±1.03% (85 runs sampled) | |
lastIndexOf() x 621,281 ops/sec ±1.31% (91 runs sampled) | |
lastIndexOf by for x 1,834,048 ops/sec ±2.22% (88 runs sampled) | |
lastIndexOf by while x 1,451,324 ops/sec ±1.10% (90 runs sampled) | |
Array.prototype.indexOf.call x 1,065,097 ops/sec ±1.30% (89 runs sampled) | |
Array.prototype.lastIndexOf.call x 554,862 ops/sec ±3.16% (83 runs sampled) | |
Fastest is indexOf() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment