public
Created

  • Download Gist
benchmark.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
var Benchmark = require('benchmark')
, suite = new Benchmark.Suite();
 
var options = {
onComplete: function() {
print(this);
}
}
 
var lowerCase = [
'a', 'b', 'c', 'd', 'e', 'f', 'j', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
];
 
var upperCase = [
'A', 'B', 'C', 'D', 'E', 'F', 'J', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
];
 
var sourceArray = lowerCase.concat()
, testArray = lowerCase.concat(upperCase);
 
suite.add('unique (using Array.prototype.indexOf)', function() {
var source = sourceArray.concat()
, i = testArray.length;
 
while (i--) {
if (source.indexOf(testArray[i]) === -1)
source.push(testArray[i]);
}
 
return source;
}, options);
 
suite.add('unique (using hash to store properties)', function() {
var hash = {}
, i = sourceArray.length;
 
while (i--) {
hash[sourceArray[i]] = true;
}
 
i = testArray.length;
 
while (i--) {
hash[testArray[i]] = true;
}
 
return Object.keys(hash);
}, options);
 
suite.run();
 
function print(result) {
console.log(result.toString() + ' (' + (result.times.period * 1000.0).toFixed(3) + 'ms)');
}
output.md
Markdown
$ node benchmark.js
unique (using Array.prototype.indexOf) x 197,816 ops/sec ±0.76% (102 runs sampled) (0.005ms)
unique (using hash to store properties) x 61,929 ops/sec ±0.80% (98 runs sampled) (0.016ms)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.