Files
bare-operating-system/packages/bare-os-booter/lib/posix/bare-os-monotonic-time.js
T
2026-08-18 18:11:28 -04:00

43 lines
1011 B
JavaScript

/**
* Monotonic timing for spans (performance.now when available; else coarse wall fallback).
*/
export function bareOsMonotonicNowMs() {
if (typeof globalThis.performance?.now === 'function') {
return globalThis.performance.now()
}
return Date.now()
}
/**
* @param {number} startMs
*/
export function bareOsMonotonicElapsedMs(startMs) {
return Math.max(0, bareOsMonotonicNowMs() - startMs)
}
/**
* High-resolution time for `/proc` hints: `process.hrtime.bigint` when available, else `performance.now`.
*/
export function bareOsHrtimeSnapshot() {
try {
const hr = globalThis.process?.hrtime?.bigint
if (typeof hr === 'function') {
const ns = hr.call(globalThis.process)
return {
schema: 1,
source: 'process.hrtime.bigint',
bigintNs: ns.toString(),
atMs: Date.now()
}
}
} catch {
/* ignore */
}
return {
schema: 1,
source: 'performance.now',
monotonicMs: bareOsMonotonicNowMs(),
atMs: Date.now()
}
}