Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[minor] Added a micro typeof benchmark
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |