Skip to content

Commit 873d9bb

Browse files
authored
ref: Use Date.now() instead of new Date().getTime() (#7492)
This is slightly shorter and should actually be slightly more performant.
1 parent af0224a commit 873d9bb

File tree

14 files changed

+26
-26
lines changed

14 files changed

+26
-26
lines changed

packages/browser/test/integration/polyfills/raf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
if (!window.requestAnimationFrame)
1919
window.requestAnimationFrame = function (callback, _element) {
20-
var currTime = new Date().getTime();
20+
var currTime = Date.now();
2121
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
2222
var id = window.setTimeout(function () {
2323
callback(currTime + timeToCall);

packages/integrations/test/offline.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ function prepopulateEvents(count: number = 1): void {
194194
for (let i = 0; i < count; i++) {
195195
events.push({
196196
message: 'There was an error!',
197-
timestamp: new Date().getTime(),
197+
timestamp: Date.now(),
198198
});
199199
}
200200
}

packages/replay/src/coreHandlers/handleHistory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface HistoryHandlerData {
99
function handleHistory(handlerData: HistoryHandlerData): ReplayPerformanceEntry {
1010
const { from, to } = handlerData;
1111

12-
const now = new Date().getTime() / 1000;
12+
const now = Date.now() / 1000;
1313

1414
return {
1515
type: 'navigation.push',

packages/replay/src/replay.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class ReplayContainer implements ReplayContainerInterface {
7878
/**
7979
* Timestamp of the last user activity. This lives across sessions.
8080
*/
81-
private _lastActivity: number = new Date().getTime();
81+
private _lastActivity: number = Date.now();
8282

8383
/**
8484
* Is the integration currently active?
@@ -108,7 +108,7 @@ export class ReplayContainer implements ReplayContainerInterface {
108108
traceIds: new Set(),
109109
urls: [],
110110
earliestEvent: null,
111-
initialTimestamp: new Date().getTime(),
111+
initialTimestamp: Date.now(),
112112
initialUrl: '',
113113
};
114114

@@ -442,7 +442,7 @@ export class ReplayContainer implements ReplayContainerInterface {
442442
this._clearContext();
443443

444444
this._context.initialUrl = url;
445-
this._context.initialTimestamp = new Date().getTime();
445+
this._context.initialTimestamp = Date.now();
446446
this._context.urls.push(url);
447447
}
448448

@@ -634,14 +634,14 @@ export class ReplayContainer implements ReplayContainerInterface {
634634
/**
635635
* Update user activity (across session lifespans)
636636
*/
637-
private _updateUserActivity(_lastActivity: number = new Date().getTime()): void {
637+
private _updateUserActivity(_lastActivity: number = Date.now()): void {
638638
this._lastActivity = _lastActivity;
639639
}
640640

641641
/**
642642
* Updates the session's last activity timestamp
643643
*/
644-
private _updateSessionActivity(_lastActivity: number = new Date().getTime()): void {
644+
private _updateSessionActivity(_lastActivity: number = Date.now()): void {
645645
if (this.session) {
646646
this.session.lastActivity = _lastActivity;
647647
this._maybeSaveSession();
@@ -768,7 +768,7 @@ export class ReplayContainer implements ReplayContainerInterface {
768768
eventContext,
769769
session: this.session,
770770
options: this.getOptions(),
771-
timestamp: new Date().getTime(),
771+
timestamp: Date.now(),
772772
});
773773
} catch (err) {
774774
this._handleException(err);

packages/replay/src/session/Session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { isSampled } from '../util/isSampled';
77
* Get a session with defaults & applied sampling.
88
*/
99
export function makeSession(session: Partial<Session> & { sampled: Sampled }): Session {
10-
const now = new Date().getTime();
10+
const now = Date.now();
1111
const id = session.id || uuid4();
1212
// Note that this means we cannot set a started/lastActivity of `0`, but this should not be relevant outside of tests.
1313
const started = session.started || now;

packages/replay/src/util/addEvent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export async function addEvent(
3131
// page has been left open and idle for a long period of time and user
3232
// comes back to trigger a new session. The performance entries rely on
3333
// `performance.timeOrigin`, which is when the page first opened.
34-
if (timestampInMs + replay.timeouts.sessionIdle < new Date().getTime()) {
34+
if (timestampInMs + replay.timeouts.sessionIdle < Date.now()) {
3535
return null;
3636
}
3737

packages/replay/src/util/addMemoryEntry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function createMemoryEntry(memoryEntry: MemoryInfo): ReplayMemoryEntry {
3333
const { jsHeapSizeLimit, totalJSHeapSize, usedJSHeapSize } = memoryEntry;
3434
// we don't want to use `getAbsoluteTime` because it adds the event time to the
3535
// time origin, so we get the current timestamp instead
36-
const time = new Date().getTime() / 1000;
36+
const time = Date.now() / 1000;
3737
return {
3838
type: 'memory',
3939
name: 'memory',

packages/replay/src/util/createBreadcrumb.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function createBreadcrumb(
99
breadcrumb: Pick<Breadcrumb, RequiredProperties> & Partial<Omit<Breadcrumb, RequiredProperties>>,
1010
): Breadcrumb {
1111
return {
12-
timestamp: new Date().getTime() / 1000,
12+
timestamp: Date.now() / 1000,
1313
type: 'default',
1414
...breadcrumb,
1515
};

packages/replay/test/fixtures/error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Event } from '@sentry/types';
22

33
export function Error(obj?: Event): any {
4-
const timestamp = new Date().getTime() / 1000;
4+
const timestamp = Date.now() / 1000;
55

66
return {
77
exception: {

packages/replay/test/fixtures/transaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Event, SeverityLevel } from '@sentry/types';
22

33
export function Transaction(traceId?: string, obj?: Partial<Event>): any {
4-
const timestamp = new Date().getTime() / 1000;
4+
const timestamp = Date.now() / 1000;
55

66
return {
77
contexts: {

packages/replay/test/mocks/mockRrweb.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export type RecordMock = jest.MockedFunction<typeof rrwebRecord> & RecordAdditio
1919
function createCheckoutPayload(isCheckout: boolean = true) {
2020
return {
2121
data: { isCheckout },
22-
timestamp: new Date().getTime(),
22+
timestamp: Date.now(),
2323
type: isCheckout ? 2 : 3,
2424
};
2525
}

packages/replay/test/unit/coreHandlers/handleScope.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('Unit | coreHandlers | handleScope', () => {
2424
}
2525

2626
const testMsg = {
27-
timestamp: new Date().getTime() / 1000,
27+
timestamp: Date.now() / 1000,
2828
message: 'testing',
2929
category: 'console',
3030
};

packages/replay/test/unit/session/getSession.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const SAMPLE_RATES = {
1717
errorSampleRate: 0,
1818
};
1919

20-
function createMockSession(when: number = new Date().getTime()) {
20+
function createMockSession(when: number = Date.now()) {
2121
return makeSession({
2222
id: 'test_session_id',
2323
segmentId: 0,
@@ -66,7 +66,7 @@ describe('Unit | session | getSession', () => {
6666
});
6767

6868
it('creates a non-sticky session, regardless of session existing in sessionStorage', function () {
69-
saveSession(createMockSession(new Date().getTime() - 10000));
69+
saveSession(createMockSession(Date.now() - 10000));
7070

7171
const { session } = getSession({
7272
timeouts: {
@@ -93,8 +93,8 @@ describe('Unit | session | getSession', () => {
9393
...SAMPLE_RATES,
9494
currentSession: makeSession({
9595
id: 'old_session_id',
96-
lastActivity: new Date().getTime() - 1001,
97-
started: new Date().getTime() - 1001,
96+
lastActivity: Date.now() - 1001,
97+
started: Date.now() - 1001,
9898
segmentId: 0,
9999
sampled: 'session',
100100
}),
@@ -142,7 +142,7 @@ describe('Unit | session | getSession', () => {
142142
});
143143

144144
it('fetches an existing sticky session', function () {
145-
const now = new Date().getTime();
145+
const now = Date.now();
146146
saveSession(createMockSession(now));
147147

148148
const { session } = getSession({
@@ -168,8 +168,8 @@ describe('Unit | session | getSession', () => {
168168
});
169169

170170
it('fetches an expired sticky session', function () {
171-
const now = new Date().getTime();
172-
saveSession(createMockSession(new Date().getTime() - 2000));
171+
const now = Date.now();
172+
saveSession(createMockSession(Date.now() - 2000));
173173

174174
const { session } = getSession({
175175
timeouts: {

packages/svelte/test/performance.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe('Sentry.trackComponent()', () => {
8080
it('creates an update span, when the component is updated', async () => {
8181
// Make the finish() function actually end the initSpan
8282
testInitSpan.finish.mockImplementation(() => {
83-
testInitSpan.endTimestamp = new Date().getTime();
83+
testInitSpan.endTimestamp = Date.now();
8484
});
8585

8686
// first we create the component
@@ -171,7 +171,7 @@ describe('Sentry.trackComponent()', () => {
171171
it("doesn't record update spans, if there's no ongoing transaction at that time", async () => {
172172
// Make the finish() function actually end the initSpan
173173
testInitSpan.finish.mockImplementation(() => {
174-
testInitSpan.endTimestamp = new Date().getTime();
174+
testInitSpan.endTimestamp = Date.now();
175175
});
176176

177177
// first we create the component

0 commit comments

Comments
 (0)