UNPKG

@thi.ng/timestamp

Version:

Timestamp getter wrapping (in order of preference) `process.hrtime.bigint()`, `performance.now()` or `Date.now()`

9 lines (8 loc) 657 B
const now = typeof BigInt !== "undefined" ? typeof process !== "undefined" && typeof process.hrtime !== "undefined" && typeof process.hrtime.bigint === "function" ? () => process.hrtime.bigint() : typeof performance !== "undefined" ? () => BigInt(Math.floor(performance.now() * 1e6)) : () => BigInt(Date.now() * 1e6) : typeof performance !== "undefined" ? () => performance.now() * 1e6 : () => Date.now() * 1e6; const timeDiff = (a, b = now()) => (typeof BigInt !== "undefined" ? Number(BigInt(b) - BigInt(a)) : Number(b) - Number(a)) * 1e-6; const asMillis = (t) => (typeof t === "bigint" ? Number(t) : t) * 1e-6; export { asMillis, now, timeDiff };