Skip to content

Commit

Permalink
fix: fix browser support
Browse files Browse the repository at this point in the history
closes #171

Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
  • Loading branch information
jerome-benoit committed Oct 29, 2024
1 parent 88fab00 commit bb71e4e
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { emptyFunction, tTable } from './constants';
import type { Fn, Statistics } from './types';

// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
const isBrowser = !!(globalThis as any).navigator;

/**
* Converts nanoseconds to milliseconds.
*
Expand All @@ -17,7 +20,14 @@ export const nToMs = (ns: number) => ns / 1e6;
*/
export const mToNs = (ms: number) => ms * 1e6;

const hrtimeBigint = process.hrtime.bigint.bind(process.hrtime);
let hrtimeBigint: () => bigint;
if (isBrowser) {
hrtimeBigint = () => {
throw new Error('hrtime.bigint() is not supported in this JS environment');
};
} else {
hrtimeBigint = process.hrtime.bigint.bind(process.hrtime);
}
export const hrtimeNow = () => nToMs(Number(hrtimeBigint()));

const performanceNow = performance.now.bind(performance);
Expand Down

0 comments on commit bb71e4e

Please sign in to comment.