Skip to content

Commit

Permalink
perf: bind timestamping functions to their module
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <[email protected]>
  • Loading branch information
jerome-benoit committed Oct 20, 2024
1 parent 7e2d485 commit dffe3f0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const runtimes = {
browser: 'browser',
}

export const emptyFunction = () => {}
export const emptyFunction = Object.freeze(() => {})

export const tatamiNgGroup = '$tatami-ng_group'

Expand Down
4 changes: 0 additions & 4 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ export const version = (() => {
node: () => globalThis.process.version,
deno: () => globalThis.Deno.version.deno,
bun: () => globalThis.process.versions.bun,
// hermes: () =>
// globalThis.HermesInternal?.getRuntimeProperties?.()?.[
// 'OSS Release Version'
// ],
}[runtime]()
})()

Expand Down
23 changes: 18 additions & 5 deletions src/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export const now = (() => {

return () => 1e6 * now()
} catch {
return () => 1e6 * Date.now()
const now = Date.now.bind(Date)
return () => 1e6 * now()
}
},
browser: () => {
Expand All @@ -25,10 +26,22 @@ export const now = (() => {
return () => 1e6 * $262.agent.monotonicNow()
} catch {}

return () => 1e6 * performance.now()
return () => {
const now = performance.now.bind(performance)
return 1e6 * now()
}
},
node: () => () => {
const hrtimeNow = process.hrtime.bigint.bind(process)
return Number(hrtimeNow())
},
deno: () => () => {
const now = performance.now.bind(performance)
return 1e6 * now()
},
bun: () => {
const now = Bun.nanoseconds.bind(Bun)
return now
},
node: () => () => Number(process.hrtime.bigint()),
deno: () => () => 1e6 * performance.now(),
bun: () => Bun.nanoseconds,
}[runtime]()
})()
4 changes: 3 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { emptyFunction } from './constants.js'

/**
* Checks if a value is a promise-like object.
*
Expand Down Expand Up @@ -48,7 +50,7 @@ export const isFunctionAsyncResource = fn => {
if (promiseLike) {
// silence promise rejection
try {
fnCall.then(() => {})?.catch(() => {})
fnCall.then(emptyFunction)?.catch(emptyFunction)
} catch {
// ignore
}
Expand Down

0 comments on commit dffe3f0

Please sign in to comment.