public
Created

Date.now VS microtime.now()

  • Download Gist
timer-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
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();

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()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.