Skip to content

Commit

Permalink
Change new Date() to Date.now() to save on date allocations
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Riley Dulin authored and grabbou committed Oct 2, 2018
1 parent a1cf657 commit ddce06f
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions Libraries/BatchedBridge/MessageQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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__) {
Expand Down Expand Up @@ -141,7 +141,7 @@ class MessageQueue {
}

getEventLoopRunningTime() {
return new Date().getTime() - this._eventLoopStartTime;
return Date.now() - this._eventLoopStartTime;
}

registerCallableModule(name: string, module: Object) {
Expand Down Expand Up @@ -243,7 +243,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
Expand Down Expand Up @@ -323,7 +323,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)})`);
Expand Down Expand Up @@ -352,7 +352,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.
Expand Down

0 comments on commit ddce06f

Please sign in to comment.