Create a gist now

Instantly share code, notes, and snippets.

Date.now VS microtime.now()
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();
@fengmk2
Owner
fengmk2 commented Dec 20, 2012

Result:

$ node timer.js 
Date.now() x 4,290,064 ops/sec ±3.71% (84 runs sampled)
microtime.now() x 2,613,316 ops/sec ±3.58% (89 runs sampled)
process.uptime() x 311,993 ops/sec ±3.29% (84 runs sampled)
process.hrtime() x 678,040 ops/sec ±2.89% (89 runs sampled)
Fastest is Date.now()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment