Created
August 17, 2020 06:59
-
-
Save cameroncondry/90efdc58dd1099be1dcc6cc91d71e328 to your computer and use it in GitHub Desktop.
Simple JavaScript benchmarking function for simple comparisons
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
| /** | |
| * A straightforward Node benchmarking script for performance testing | |
| * using a similar method to jsPerf. | |
| * | |
| * Usage: `node ./performance.js` | |
| * | |
| * Can support any number of tests, with optional startup and teardown. | |
| */ | |
| const Benchmark = require('benchmark'); | |
| let tests = { | |
| 'RegExp#test': () => { | |
| /o/.test('Hello World!'); | |
| }, | |
| 'String#indexOf': () => { | |
| 'Hello World!'.indexOf('o') > -1; | |
| } | |
| }; | |
| let onStart = () => {}; | |
| let onComplete = () => {}; | |
| doBenchmarkTesting(); | |
| function doBenchmarkTesting() { | |
| let suite = new Benchmark.Suite('performance', { | |
| 'async': true, | |
| 'queued': true, | |
| 'onCycle': (event) => console.log(String(event.target)), | |
| 'onComplete': () => console.log('Fastest is ' + suite.filter('fastest').map('name')) | |
| }); | |
| let keyMax = Math.max(...Object.keys(tests).map(item => item.length)); | |
| for (const key in tests) { | |
| if (!tests.hasOwnProperty(key)) continue; | |
| suite.add(key.padEnd(keyMax + 1), { | |
| 'fn': tests[key], | |
| 'onStart': onStart, | |
| 'onComplete': onComplete | |
| }); | |
| } | |
| suite.run(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment