Skip to content

ref(core): Avoid side-effect of & streamline timestampInSeconds method #16890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ sentryTest.describe('When `consistentTraceSampling` is `true` and page contains
sentryTest.skip();
}




const url = await getLocalTestUrl({ testDir: __dirname });

const clientReportPromise = waitForClientReportRequest(page);
Expand Down
17 changes: 11 additions & 6 deletions packages/core/src/utils/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@ export function dateTimestampInSeconds(): number {
*/
function createUnixTimestampInSecondsFunc(): () => number {
const { performance } = GLOBAL_OBJ as typeof GLOBAL_OBJ & { performance?: Performance };
if (!performance?.now) {
// Some browser and environments don't have a performance or timeOrigin, so we fallback to
// using Date.now() to compute the starting time.
if (!performance?.now || !performance.timeOrigin) {
return dateTimestampInSeconds;
}

// Some browser and environments don't have a timeOrigin, so we fallback to
// using Date.now() to compute the starting time.
const approxStartingTimeOrigin = Date.now() - performance.now();
const timeOrigin = performance.timeOrigin == undefined ? approxStartingTimeOrigin : performance.timeOrigin;
const timeOrigin = performance.timeOrigin;

// performance.now() is a monotonic clock, which means it starts at 0 when the process begins. To get the current
// wall clock time (actual UNIX timestamp), we need to add the starting time origin and the current time elapsed.
Expand All @@ -55,6 +54,8 @@ function createUnixTimestampInSecondsFunc(): () => number {
};
}

let _cachedTimestampInSeconds: (() => number) | undefined;

/**
* Returns a timestamp in seconds since the UNIX epoch using either the Performance or Date APIs, depending on the
* availability of the Performance API.
Expand All @@ -64,7 +65,11 @@ function createUnixTimestampInSecondsFunc(): () => number {
* skew can grow to arbitrary amounts like days, weeks or months.
* See https://github.com/getsentry/sentry-javascript/issues/2590.
*/
export const timestampInSeconds = createUnixTimestampInSecondsFunc();
export function timestampInSeconds(): number {
// We store this in a closure so that we don't have to create a new function every time this is called.
const func = _cachedTimestampInSeconds ?? (_cachedTimestampInSeconds = createUnixTimestampInSecondsFunc());
return func();
}

/**
* Cached result of getBrowserTimeOrigin.
Expand Down
Loading