Skip to content

Commit 758722e

Browse files
ref: Prevent instantiating unnecessary Date objects (#2442)
* ref: Prevent instantiating unnecessary Date objects Use Date.now() instead of new Date().getTime(). Resources: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now * fixed tests Co-authored-by: Kamil Ogórek <[email protected]>
1 parent 4bdb330 commit 758722e

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

packages/node/test/transports/http.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,11 @@ describe('HTTPTransport', () => {
9898
const now = Date.now();
9999
const mock = jest
100100
.spyOn(Date, 'now')
101+
// Initialize _disabledUntil attribute
102+
.mockReturnValueOnce(now)
101103
// Check for first event
102104
.mockReturnValueOnce(now)
103-
// Setting disableUntil
105+
// Setting disabledUntil
104106
.mockReturnValueOnce(now)
105107
// Check for second event
106108
.mockReturnValueOnce(now + (retryAfterSeconds / 2) * 1000)

packages/node/test/transports/https.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,11 @@ describe('HTTPSTransport', () => {
104104
const now = Date.now();
105105
const mock = jest
106106
.spyOn(Date, 'now')
107+
// Initialize _disabledUntil attribute
108+
.mockReturnValueOnce(now)
107109
// Check for first event
108110
.mockReturnValueOnce(now)
109-
// Setting disableUntil
111+
// Setting disabledUntil
110112
.mockReturnValueOnce(now)
111113
// Check for second event
112114
.mockReturnValueOnce(now + (retryAfterSeconds / 2) * 1000)

packages/utils/src/misc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ function _htmlElementAsString(el: unknown): string {
350350
* Returns a timestamp in seconds with milliseconds precision.
351351
*/
352352
export function timestampWithMs(): number {
353-
return new Date().getTime() / 1000;
353+
return Date.now() / 1000;
354354
}
355355

356356
// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string

0 commit comments

Comments
 (0)