Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save samuelcarreira/dad1f819606b37022703713a16b0fa8b to your computer and use it in GitHub Desktop.
Save samuelcarreira/dad1f819606b37022703713a16b0fa8b to your computer and use it in GitHub Desktop.
Benchmark time subtraction: process.hrtime; process.hrtime.bigint(); Date.now()
/**
* I know that the process.hrtime as a higher precision over the
* Date constructor object so it's not a "fair" benchmark,
* but if you doesn't need a high precision timer and you just want
* to compare the fastest time subtraction this benchmark can
* be usefull
*
* Licensed under MIT
* Copyright (c) 2020 [Samuel Carreira]
*/
const benchmark = require('nodemark');
const os = require('os');
function dateNow() {
const t = Date.now();
return Date.now() - t; // milliseconds
}
function hrTimeBigInt() {
const t = process.hrtime.bigint();
return process.hrtime.bigint() - t; // nanoseconds
}
function hrTime() {
const t = process.hrtime();
const diff = process.hrtime(t);
return diff[0] * 1e9 + diff[1]; // nanoseconds
}
console.log(`Node ${process.version}`);
console.log(`CPU ${os.cpus()[0].model}`);
console.log('process.hrtime: ' + benchmark(hrTime));
console.log('process.hrtime.bigint(): ' + benchmark(hrTimeBigInt));
console.log('Date.now(): ' + benchmark(dateNow));
@thmang82
Copy link

thmang82 commented Dec 28, 2020

Very interesting benchmark! Results on a Raspberry Pi 4 with current Node 14 look a bit different:

Node v14.15.3
CPU ARMv7 Processor rev 3 (v7l)
process.hrtime: 390,346 ops/sec ±1.4% (436235 samples)
process.hrtime.bigint(): 387,148 ops/sec ±0.51% (440785 samples)
Date.now(): 722,214 ops/sec ±0.16% (549147 samples)

Node v15.5.0
CPU ARMv7 Processor rev 3 (v7l)
process.hrtime: 552,216 ops/sec ±0.74% (630562 samples)
process.hrtime.bigint(): 533,725 ops/sec ±0.34% (618925 samples)
Date.now(): 747,393 ops/sec ±0.31% (711148 samples)

Date.now() is factor two (!) faster compared to to process.hrtime in Node 14. Node 15 improves stuff quite a bit.

One point to consider: Date.now() might "jump" in case the system time is updated. If you really want precise duration measurements, process.hrtime should be the better option.

@thmang82
Copy link

thmang82 commented Dec 28, 2020

Some more results from a MacBook Pro 15 inch from 2012:

Node v12.16.1
CPU Intel(R) Core(TM) i7-3615QM CPU @ 2.30GHz
process.hrtime: 3,380,517 ops/sec ±0.31% (4056543 samples)
process.hrtime.bigint(): 2,379,471 ops/sec ±0.27% (3469938 samples)
Date.now(): 4,137,538 ops/sec ±0.25% (4440286 samples)

Node v14.15.3
CPU Intel(R) Core(TM) i7-3615QM CPU @ 2.30GHz
process.hrtime: 2,308,075 ops/sec ±0.32% (2647063 samples)
process.hrtime.bigint(): 1,701,060 ops/sec ±0.23% (2307094 samples)
Date.now(): 4,058,047 ops/sec ±0.3% (3174998 samples)

Node v15.5.0
CPU Intel(R) Core(TM) i7-3615QM CPU @ 2.30GHz
process.hrtime: 3,681,846 ops/sec ±0.31% (4296021 samples)
process.hrtime.bigint(): 2,837,321 ops/sec ±0.29% (3879987 samples)
Date.now(): 3,836,750 ops/sec ±0.3% (4375058 samples)

This is quite interesting: Node 14 is actually slower as node 12, while Node 15 compensates the 2x disadvantage of process.hrtime found in earlier versions

@romainwurtz
Copy link

Run 1:
Node v18.13.0
CPU Apple M1 Max
process.hrtime: 14,693,093 ops/sec ±0.42% (16188265 samples)
process.hrtime.bigint(): 12,370,171 ops/sec ±0.29% (16894424 samples)
Date.now(): 12,599,143 ops/sec ±0.33% (15674109 samples)

Run 2:
Node v18.13.0
CPU Apple M1 Max
process.hrtime: 14,652,087 ops/sec ±0.43% (16216034 samples)
process.hrtime.bigint(): 12,301,886 ops/sec ±0.28% (16976581 samples)
Date.now(): 12,799,446 ops/sec ±0.31% (15855477 samples)

Interesting results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment