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: {

0 commit comments

Comments
 (0)