Created
August 23, 2017 01:15
-
-
Save lucyllewy/e6537530eeeababb40a4c1cfb7544b4f 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
const Benchmark = require('benchmark') | |
const WARN_RANGE = 5 | |
const suite = new Benchmark.Suite({ | |
onCycle (event) { | |
const bench = event.target | |
const prev = this.previous && this.previous[bench.name] | |
const pctDelta = prev && (((bench.stats.mean - prev.stats.mean) / prev.stats.mean) * 100) | |
let colorDiff = !prev | |
? '' | |
: `${pctDelta > 0 ? '+' : ''}${pctDelta.toFixed(2)}% ` | |
colorDiff = ` (${ | |
pctDelta >= (WARN_RANGE + bench.stats.rme) | |
? chalk.red(colorDiff) | |
: pctDelta <= -(WARN_RANGE + bench.stats.rme) | |
? chalk.green(colorDiff) | |
: colorDiff | |
}±${bench.stats.rme.toFixed(2)}%)` | |
console.log('------------------------------------------------') | |
console.log(` ${bench.name}`) | |
console.log('------------------------------------------------') | |
if (bench.error) { | |
console.log('Error:', bench.error.message || bench.error) | |
} else { | |
console.log(` ${ | |
bench.hz.toFixed(bench.hz < 100 ? 2 : 0) | |
} ops/s @ ~${ | |
(bench.stats.mean * 1000).toFixed(3) | |
}ms/op${colorDiff}`) | |
console.log(` Sampled ${ | |
bench.stats.sample.length | |
} in ${ | |
bench.times.elapsed.toFixed(2)}s.`) | |
} | |
}, | |
onComplete() { | |
console.log('finished') | |
} | |
}).add('worstcase replace regex', { | |
onStart() { | |
this.first = true | |
this.longString = ""; | |
this.worstcase = ""; | |
// Large = 66,000 | |
for (var i = 0; i < 66000; i++) { | |
this.longString += i % 10; | |
} | |
for (var i=0; i<66000; i += 14) { | |
this.worstcase += '<script>' ; | |
} | |
this.re = /<script>/g; | |
}, | |
fn() { | |
this.worstcase.replace(this.re, '<script>') | |
} | |
}).add('replace regex', { | |
onStart() { | |
this.first = true | |
this.longString = ""; | |
this.worstcase = ""; | |
// Large = 66,000 | |
for (var i = 0; i < 66000; i++) { | |
this.longString += i % 10; | |
} | |
for (var i=0; i<66000; i += 14) { | |
this.worstcase += '<script>' ; | |
} | |
this.re = /<script>/g; | |
}, | |
fn() { | |
this.longString.replace(this.re, '<script>') | |
} | |
}).add('substring', { | |
onStart() { | |
this.first = true | |
this.longString = ""; | |
this.worstcase = ""; | |
// Large = 66,000 | |
for (var i = 0; i < 66000; i++) { | |
this.longString += i % 10; | |
} | |
for (var i=0; i<66000; i += 14) { | |
this.worstcase += '<script>' ; | |
} | |
this.re = /<script>/g; | |
}, | |
fn() { | |
this.longString.substring(0, this.longString.length - 10) | |
} | |
}).add('slice', { | |
onStart() { | |
this.first = true | |
this.longString = ""; | |
this.worstcase = ""; | |
// Large = 66,000 | |
for (var i = 0; i < 66000; i++) { | |
this.longString += i % 10; | |
} | |
for (var i=0; i<66000; i += 14) { | |
this.worstcase += '<script>' ; | |
} | |
this.re = /<script>/g; | |
}, | |
fn() { | |
this.longString.slice(0, -10) | |
} | |
}).add('substr', { | |
onStart() { | |
this.first = true | |
this.longString = ""; | |
this.worstcase = ""; | |
// Large = 66,000 | |
for (var i = 0; i < 66000; i++) { | |
this.longString += i % 10; | |
} | |
for (var i=0; i<66000; i += 14) { | |
this.worstcase += '<script>' ; | |
} | |
this.re = /<script>/g; | |
}, | |
fn() { | |
this.longString.substr(0, this.longString.length - 10) | |
} | |
}).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
{ | |
"name": "benchmarking-strings", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "MIT", | |
"dependencies": { | |
"benchmark": "^2.1.4" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment