Created
December 20, 2012 14:32
-
-
Save fengmk2/4345606 to your computer and use it in GitHub Desktop.
Date.now VS microtime.now()
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
var Benchmark = require('benchmark'); | |
var microtime = require('microtime'); | |
var suite = new Benchmark.Suite(); | |
suite | |
.add('Date.now()', function () { | |
var diff1 = Date.now() - Date.now(); | |
}) | |
.add('microtime.now()', function () { | |
var diff2 = microtime.now() - microtime.now(); | |
}) | |
.add('process.uptime()', function () { | |
var diff3 = process.uptime() - process.uptime(); | |
}) | |
.add('process.hrtime()', function () { | |
var t = process.hrtime(); | |
var diff4 = process.hrtime(t); | |
}) | |
// add listeners | |
.on('cycle', function (event, bench) { | |
console.log(String(bench)); | |
}) | |
.on('complete', function () { | |
console.log('Fastest is ' + this.filter('fastest').pluck('name')); | |
}) | |
.run(); |
$ node -v
v8.12.0
$ node timer.js
Date.now() x 6,456,741 ops/sec ±0.91% (89 runs sampled)
microtime.now() x 5,361,535 ops/sec ±0.87% (92 runs sampled)
process.uptime() x 3,414,882 ops/sec ±1.37% (86 runs sampled)
process.hrtime() x 5,982,966 ops/sec ±1.22% (87 runs sampled)
Fastest is Date.now()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result: