Skip to content

Instantly share code, notes, and snippets.

@mucaho
Last active February 8, 2022 00:18
Show Gist options
  • Save mucaho/d9a71cbe3f05200e136c to your computer and use it in GitHub Desktop.
Save mucaho/d9a71cbe3f05200e136c to your computer and use it in GitHub Desktop.
Generic template for benchmarking / performance testing JavaScript code using the [Benchmark node module](https://www.npmjs.com/package/benchmark)
<html>
<head>
<script src="https://wzrd.in/bundle/[email protected]"></script>
<script src="https://wzrd.in/bundle/[email protected]"></script>
<script src="https://wzrd.in/bundle/[email protected]"></script>
</head>
<body>
<script>
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
// add tests
suite.add('FirstTest', function() {
// first test here
})
.add('SecondTest', function() {
// second test here
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run tests async
.run({ 'async': true });
</script>
</body>
</html>
@thybzi
Copy link

thybzi commented Mar 26, 2016

Doesn't work without including lodash:
<script src="https://cdn.jsdelivr.net/lodash/4.6.1/lodash.min.js"></script>

@thybzi
Copy link

thybzi commented Mar 26, 2016

Also, pluck seems to be undefined for me.
Changed to:
console.log('Fastest is ' + this.filter('fastest')[0].name);

@mucaho
Copy link
Author

mucaho commented Apr 29, 2016

@thybzi Thanks, Benchmark changed their API a bit with new version, I went ahead updated the example and also restricted the versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment