Skip to content

Instantly share code, notes, and snippets.

@fengmk2
Created December 20, 2012 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fengmk2/4345606 to your computer and use it in GitHub Desktop.
Save fengmk2/4345606 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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

@danthegoodman
Copy link

$ 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