Skip to content

Commit

Permalink
fix(bench): update now() to only OPTIONALLY use bigint timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Dec 21, 2019
1 parent aa4faed commit 7ac391b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 25 deletions.
18 changes: 8 additions & 10 deletions packages/bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ This project is part of the

- [About](#about)
- [Status](#status)
- [Breaking changes](#breaking-changes)
- [Installation](#installation)
- [Dependencies](#dependencies)
- [Usage examples](#usage-examples)
Expand All @@ -24,19 +23,18 @@ This project is part of the

Benchmarking utilities w/ optional statistics.

Though no public API change (only additions), since v2.0.0 this library
internally attempts to use high-res ES
[`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)
timestamps (in Node via
[`process.hrtime.bigint()`](https://nodejs.org/dist/latest-v12.x/docs/api/process.html#process_process_hrtime_bigint)).
If `BigInt` is not available in the target environment, timestamps are
still only sourced via `Date.now()`.

### Status

**STABLE** - used in production

### Breaking changes

Though no public API change, since v2.0.0 this library internally uses
ES
[`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)
timestamps (in Node via `process.hrtime.bigint()`). See
[caniuse](https://caniuse.com/#feat=mdn-javascript_builtins_bigint) for
browser support.

## Installation

```bash
Expand Down
17 changes: 8 additions & 9 deletions packages/bench/README.tpl.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ This project is part of the

${pkg.description}

${status}

### Breaking changes

Though no public API change, since v2.0.0 this library internally uses
ES
Though no public API change (only additions), since v2.0.0 this library
internally attempts to use high-res ES
[`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)
timestamps (in Node via `process.hrtime.bigint()`). See
[caniuse](https://caniuse.com/#feat=mdn-javascript_builtins_bigint) for
browser support.
timestamps (in Node via
[`process.hrtime.bigint()`](https://nodejs.org/dist/latest-v12.x/docs/api/process.html#process_process_hrtime_bigint)).
If `BigInt` is not available in the target environment, timestamps are
still only sourced via `Date.now()`.

${status}

${supportPackages}

Expand Down
14 changes: 9 additions & 5 deletions packages/bench/src/now.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
let M: bigint;

/**
* If available, returns wrapper for `process.hrtime.bigint()` else
* falls back to `Date.now()` (scaled to nanoseconds). In all cases,
* returns a `bigint` timestamp.
*/
export const now =
typeof process !== "undefined" &&
typeof process.hrtime !== "undefined" &&
typeof process.hrtime.bigint === "function"
? () => process.hrtime.bigint()
: () => BigInt(Date.now()) * 1_000_000n;
typeof BigInt !== "undefined"
? typeof process !== "undefined" &&
typeof process.hrtime !== "undefined" &&
typeof process.hrtime.bigint === "function"
? () => process.hrtime.bigint()
: ((M = BigInt(1_000_000)), () => BigInt(Date.now()) * M)
: () => Date.now();
8 changes: 7 additions & 1 deletion packages/bench/src/timed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ export const timed = <T>(fn: () => T, prefix = "") => {
export const timedResult = <T>(fn: () => T): TimingResult<T> => {
const t0 = now();
const res = fn();
return [res, Number(now() - t0) * 1e-6];
const t1 = now();
return [
res,
typeof BigInt !== "undefined"
? Number(<bigint>t1 - <bigint>t0) * 1e-6
: <number>t1 - <number>t0
];
};

0 comments on commit 7ac391b

Please sign in to comment.