Skip to content

Instantly share code, notes, and snippets.

@TheAthlete
Created August 15, 2020 07:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheAthlete/893eb4770feeb4603d632178328a18ce to your computer and use it in GitHub Desktop.
Save TheAthlete/893eb4770feeb4603d632178328a18ce to your computer and use it in GitHub Desktop.
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
let dominoes = [[1,1],[2,2],[1,1],[1,2],[1,2],[1,1]];
// add tests
suite.add('sort-join', function() {
for (let d of dominoes) {
[...d].sort((a, b) => a - b).join('');
}
})
.add('ternary', function() {
for (let d of dominoes) {
d[0] < d[1] ? d[0] + '' + d[1] : d[1] + '' + d[0];
}
})
.add('ternary-numeric', function() {
for (let d of dominoes) {
d[0] < d[1] ? d[0] + d[1] : d[1] + d[0];
}
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({ 'async': true });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment