From bbb2d9a5b358bc0c150fe6ff74c45594c987e949 Mon Sep 17 00:00:00 2001 From: Riley Dulin Date: Fri, 21 Sep 2018 10:59:19 -0700 Subject: [PATCH] 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 --- Libraries/BatchedBridge/MessageQueue.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Libraries/BatchedBridge/MessageQueue.js b/Libraries/BatchedBridge/MessageQueue.js index 3912be6205bf3e..a7f1f5a3a5aacd 100644 --- a/Libraries/BatchedBridge/MessageQueue.js +++ b/Libraries/BatchedBridge/MessageQueue.js @@ -60,7 +60,7 @@ class MessageQueue { this._failureCallbacks = {}; this._callID = 0; this._lastFlush = 0; - this._eventLoopStartTime = new Date().getTime(); + this._eventLoopStartTime = Date.now(); this._immediatesCallback = null; if (__DEV__) { @@ -141,7 +141,7 @@ class MessageQueue { } getEventLoopRunningTime() { - return new Date().getTime() - this._eventLoopStartTime; + return Date.now() - this._eventLoopStartTime; } registerCallableModule(name: string, module: Object) { @@ -260,7 +260,7 @@ class MessageQueue { } this._queue[PARAMS].push(params); - const now = new Date().getTime(); + const now = Date.now(); if ( global.nativeFlushQueueImmediate && now - this._lastFlush >= MIN_TIME_BETWEEN_FLUSHES_MS @@ -340,7 +340,7 @@ class MessageQueue { } __callFunction(module: string, method: string, args: any[]): any { - this._lastFlush = new Date().getTime(); + this._lastFlush = Date.now(); this._eventLoopStartTime = this._lastFlush; if (__DEV__ || this.__spy) { Systrace.beginEvent(`${module}.${method}(${stringifySafe(args)})`); @@ -369,7 +369,7 @@ class MessageQueue { } __invokeCallback(cbID: number, args: any[]) { - this._lastFlush = new Date().getTime(); + this._lastFlush = Date.now(); this._eventLoopStartTime = this._lastFlush; // The rightmost bit of cbID indicates fail (0) or success (1), the other bits are the callID shifted left.