80 lines
1.8 KiB
JavaScript
80 lines
1.8 KiB
JavaScript
|
import { summary, run, bench } from 'mitata';
|
||
|
|
||
|
// Warmup (de-optimize `bench()` calls)
|
||
|
bench('noop', () => { });
|
||
|
bench('noop2', () => { });
|
||
|
|
||
|
// Example benchmark
|
||
|
summary(() => {
|
||
|
const dataset = new Array(100).fill(0).map(
|
||
|
() => new Array(10).fill(0).map(() => String.fromCharCode(97 + Math.round(Math.random() * 22))).join('')
|
||
|
);
|
||
|
console.log(dataset)
|
||
|
|
||
|
const fn1 = (str) => (str[0] === 'c' ? 1 : 0) + (str[1] === 'c' ? 1 : 0) + (str[2] === 'c' ? 1 : 0);
|
||
|
fn1('a');
|
||
|
fn1('b');
|
||
|
fn1('c');
|
||
|
fn1('d');
|
||
|
// optimizeNextInvocation(fn1);
|
||
|
bench('Char check', () => dataset.map(fn1))
|
||
|
|
||
|
const fn2 = (str) => (str.charCodeAt(0) === 99 ? 1 : 0) + (str.charCodeAt(1) === 99 ? 1 : 0) + (str.charCodeAt(2) === 99 ? 1 : 0);
|
||
|
fn2('a');
|
||
|
fn2('b');
|
||
|
fn2('c');
|
||
|
fn2('d');
|
||
|
// optimizeNextInvocation(fn2);
|
||
|
bench('Char code check', () => dataset.map(fn2));
|
||
|
|
||
|
|
||
|
bench('2x Char check', () => dataset.map(fn1))
|
||
|
bench('2x Char code check', () => dataset.map(fn2));
|
||
|
});
|
||
|
|
||
|
// Example benchmark
|
||
|
summary(() => {
|
||
|
let paths = [
|
||
|
'test1/',
|
||
|
'test/',
|
||
|
'test3/',
|
||
|
'test/',
|
||
|
'test5/',
|
||
|
'something/',
|
||
|
'else/',
|
||
|
'goes/',
|
||
|
'here/',
|
||
|
'too/',
|
||
|
]
|
||
|
|
||
|
function fastCheck(str) {
|
||
|
if (str.charCodeAt(0) === 116) {
|
||
|
if (str.charCodeAt(1) === 101) {
|
||
|
if (str.charCodeAt(2) === 115) {
|
||
|
if (str.charCodeAt(3) === 116) {
|
||
|
if (str.charCodeAt(4) === 47) {
|
||
|
if (str.indexOf('/', 5) === -1) {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
let r = new RegExp('^test/$')
|
||
|
function regexCheck(str) {
|
||
|
return r.test(str)
|
||
|
}
|
||
|
|
||
|
console.log(paths.map(fastCheck))
|
||
|
console.log(paths.map(regexCheck))
|
||
|
|
||
|
bench('fastCheck', () => paths.map(fastCheck))
|
||
|
bench('regexCheck', () => paths.map(regexCheck));
|
||
|
|
||
|
});
|
||
|
|
||
|
run();
|