Change new Date() to Date.now() to save on date allocations

Summary:
`new Date().getTime()` is equal to `Date.now()`.
`Date.now()` avoids an allocation, which can save some GC time.

This micro-optimization isn't worth it in most places, but since MessageQueue is one of the hottest pieces of JS in RN, it's worth it.

Reviewed By: javache

Differential Revision: D9972334

fbshipit-source-id: 05d78fd65304f0f27115d76b8b52db11a52c86a0
This commit is contained in:
Riley Dulin 2018-09-21 10:59:19 -07:00 committed by Facebook Github Bot
parent f409fd8d6e
commit bbb2d9a5b3
1 changed files with 5 additions and 5 deletions

View File

@ -60,7 +60,7 @@ class MessageQueue {
this._failureCallbacks = {}; this._failureCallbacks = {};
this._callID = 0; this._callID = 0;
this._lastFlush = 0; this._lastFlush = 0;
this._eventLoopStartTime = new Date().getTime(); this._eventLoopStartTime = Date.now();
this._immediatesCallback = null; this._immediatesCallback = null;
if (__DEV__) { if (__DEV__) {
@ -141,7 +141,7 @@ class MessageQueue {
} }
getEventLoopRunningTime() { getEventLoopRunningTime() {
return new Date().getTime() - this._eventLoopStartTime; return Date.now() - this._eventLoopStartTime;
} }
registerCallableModule(name: string, module: Object) { registerCallableModule(name: string, module: Object) {
@ -260,7 +260,7 @@ class MessageQueue {
} }
this._queue[PARAMS].push(params); this._queue[PARAMS].push(params);
const now = new Date().getTime(); const now = Date.now();
if ( if (
global.nativeFlushQueueImmediate && global.nativeFlushQueueImmediate &&
now - this._lastFlush >= MIN_TIME_BETWEEN_FLUSHES_MS now - this._lastFlush >= MIN_TIME_BETWEEN_FLUSHES_MS
@ -340,7 +340,7 @@ class MessageQueue {
} }
__callFunction(module: string, method: string, args: any[]): any { __callFunction(module: string, method: string, args: any[]): any {
this._lastFlush = new Date().getTime(); this._lastFlush = Date.now();
this._eventLoopStartTime = this._lastFlush; this._eventLoopStartTime = this._lastFlush;
if (__DEV__ || this.__spy) { if (__DEV__ || this.__spy) {
Systrace.beginEvent(`${module}.${method}(${stringifySafe(args)})`); Systrace.beginEvent(`${module}.${method}(${stringifySafe(args)})`);
@ -369,7 +369,7 @@ class MessageQueue {
} }
__invokeCallback(cbID: number, args: any[]) { __invokeCallback(cbID: number, args: any[]) {
this._lastFlush = new Date().getTime(); this._lastFlush = Date.now();
this._eventLoopStartTime = this._lastFlush; this._eventLoopStartTime = this._lastFlush;
// The rightmost bit of cbID indicates fail (0) or success (1), the other bits are the callID shifted left. // The rightmost bit of cbID indicates fail (0) or success (1), the other bits are the callID shifted left.