-
-
Save tmcw/b62db4b8ec5fffa39b6ac4cff7c056ff 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 suite = Benchmark.Suite('equal') | |
const cmp = Array.from({ length: 10000 }, (_, i) => i.toString()); | |
const cmpEqual = [...cmp]; | |
const cmpNotEqual = Array.from({ length: 10000 }, (_, i) => i.toString(16)); | |
const cmpDifferentLength = Array.from({ length: 10000 }, (_, i) => i.toString(16)); | |
const cmpShort = Array.from({ length: 10 }, (_, i) => i.toString()); | |
const cmpShortEqual = [...cmpShort]; | |
function joinEqual(a, b) { | |
return a.join() === b.join(); | |
} | |
function iterativeEqual(a, b) { | |
if (a === b) return true; | |
if (a.length !== b.length) return false; | |
for (let i = 0; i < a.length; i++) { | |
if (a[i] !== b[i]) { | |
return false; | |
} | |
} | |
return true; | |
} | |
suite | |
.add('joinEqual/short', function() { | |
joinEqual(cmpShort, cmpShortEqual); | |
}) | |
.add('joinEqual/equal', function() { | |
joinEqual(cmp, cmpEqual); | |
}) | |
.add('joinEqual/notEqual', function() { | |
joinEqual(cmp, cmpNotEqual); | |
}) | |
.add('joinEqual/differentLength', function() { | |
joinEqual(cmp, cmpDifferentLength); | |
}) | |
.add('iterativeEqual/short', function() { | |
iterativeEqual(cmpShort, cmpShortEqual); | |
}) | |
.add('iterativeEqual/equal', function() { | |
iterativeEqual(cmp, cmpEqual); | |
}) | |
.add('iterativeEqual/notEqual', function() { | |
iterativeEqual(cmp, cmpNotEqual); | |
}) | |
.add('iterativeEqual/differentLength', function() { | |
iterativeEqual(cmp, cmpDifferentLength); | |
}) | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
// run async | |
.run({ 'async': true }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment