From 1d5e346c80c866604c721b02914709118a726f6d Mon Sep 17 00:00:00 2001 From: 3rd-Eden Date: Sun, 27 Jan 2013 14:53:14 +0100 Subject: [PATCH] [minor] Added a micro typeof benchmark --- benchmark/micro/typeof.bench.js | 68 +++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 benchmark/micro/typeof.bench.js diff --git a/benchmark/micro/typeof.bench.js b/benchmark/micro/typeof.bench.js new file mode 100644 index 0000000..d376686 --- /dev/null +++ b/benchmark/micro/typeof.bench.js @@ -0,0 +1,68 @@ +'use strict'; + +/** + * Benchmark related modules. + */ +var benchmark = require('benchmark') + , microtime = require('microtime'); + +/** + * Logger + */ +var logger = new(require('devnull'))({ timestamp: false, namespacing: 0 }); + +/** + * Preparation code. + */ +var bool = true + , toString = Object.prototype.toString; + +( + new benchmark.Suite() +).add('typeof equal', function test1() { + var x; + + if (typeof bool === 'boolean') { + x = bool; + } else if (typeof bool === 'number') { + x = 1; + } +}).add('typeof charAt', function test2() { + var x; + + if ((typeof bool).charCodeAt(0) === 98) { + x = bool; + } else if ((typeof bool).charCodeAt(0) === 110) { + x = 1; + } +}).add('toString equal', function test2() { + var x; + + if (toString.call(bool) === '[object Boolean]') { + x = bool; + } else if (toString.call(bool) === '[object Number]') { + x = 1; + } +}).add('typeof charAt', function test2() { + var x; + + if (toString.call(bool).charCodeAt(8) === 66) { + x = bool; + } else if (toString.call(bool).charCodeAt(8) === 78) { + x = 1; + } +}).on('cycle', function cycle(e) { + var details = e.target; + + logger.log('Finished benchmarking: "%s"', details.name); + logger.metric('Count (%d), Cycles (%d), Elapsed (%d), Hz (%d)' + , details.count + , details.cycles + , details.times.elapsed + , details.hz + ); +}).on('complete', function completed() { + logger.info('Benchmark: "%s" is was the fastest.' + , this.filter('fastest').pluck('name') + ); +}).run();