Skip to content

Commit 637231e

Browse files
authored
Merge pull request #5445 from Tyriar/tyriar/perf
Prefer performance.now() over Date.now()
2 parents e1023d7 + 6468a78 commit 637231e

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

src/browser/TimeBasedDebouncer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class TimeBasedDebouncer implements IRenderDebouncer {
4545

4646
// Only refresh if the time since last refresh is above a threshold, otherwise wait for
4747
// enough time to pass before refreshing again.
48-
const refreshRequestTime: number = Date.now();
48+
const refreshRequestTime: number = performance.now();
4949
if (refreshRequestTime - this._lastRefreshMs >= this._debounceThresholdMS) {
5050
// Enough time has lapsed since the last refresh; refresh immediately
5151
this._lastRefreshMs = refreshRequestTime;
@@ -57,7 +57,7 @@ export class TimeBasedDebouncer implements IRenderDebouncer {
5757
this._additionalRefreshRequested = true;
5858

5959
this._refreshTimeoutID = window.setTimeout(() => {
60-
this._lastRefreshMs = Date.now();
60+
this._lastRefreshMs = performance.now();
6161
this._innerRefresh();
6262
this._additionalRefreshRequested = false;
6363
this._refreshTimeoutID = undefined; // No longer need to clear the timeout

src/common/TaskQueue.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ abstract class TaskQueue implements ITaskQueue {
7474
let lastDeadlineRemaining = deadline.timeRemaining();
7575
let deadlineRemaining = 0;
7676
while (this._i < this._tasks.length) {
77-
taskDuration = Date.now();
77+
taskDuration = performance.now();
7878
if (!this._tasks[this._i]()) {
7979
this._i++;
8080
}
81-
// other than performance.now, Date.now might not be stable (changes on wall clock changes),
82-
// this is not an issue here as a clock change during a short running task is very unlikely
83-
// in case it still happened and leads to negative duration, simply assume 1 msec
84-
taskDuration = Math.max(1, Date.now() - taskDuration);
81+
// other than performance.now, performance.now might not be stable (changes on wall clock
82+
// changes), this is not an issue here as a clock change during a short running task is very
83+
// unlikely in case it still happened and leads to negative duration, simply assume 1 msec
84+
taskDuration = Math.max(1, performance.now() - taskDuration);
8585
longestTask = Math.max(taskDuration, longestTask);
8686
// Guess the following task will take a similar time to the longest task in this batch, allow
8787
// additional room to try avoid exceeding the deadline
@@ -116,9 +116,9 @@ export class PriorityTaskQueue extends TaskQueue {
116116
}
117117

118118
private _createDeadline(duration: number): ITaskDeadline {
119-
const end = Date.now() + duration;
119+
const end = performance.now() + duration;
120120
return {
121-
timeRemaining: () => Math.max(0, end - Date.now())
121+
timeRemaining: () => Math.max(0, end - performance.now())
122122
};
123123
}
124124
}

src/common/input/WriteBuffer.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export class WriteBuffer extends Disposable {
137137
* effectively lowering the redrawing needs, schematically:
138138
*
139139
* macroTask _innerWrite:
140-
* if (Date.now() - (lastTime | 0) < WRITE_TIMEOUT_MS):
140+
* if (performance.now() - (lastTime | 0) < WRITE_TIMEOUT_MS):
141141
* schedule microTask _innerWrite(lastTime)
142142
* else:
143143
* schedule macroTask _innerWrite(0)
@@ -158,7 +158,7 @@ export class WriteBuffer extends Disposable {
158158
* Note, for pure sync code `lastTime` and `promiseResult` have no meaning.
159159
*/
160160
protected _innerWrite(lastTime: number = 0, promiseResult: boolean = true): void {
161-
const startTime = lastTime || Date.now();
161+
const startTime = lastTime || performance.now();
162162
while (this._writeBuffer.length > this._bufferOffset) {
163163
const data = this._writeBuffer[this._bufferOffset];
164164
const result = this._action(data, promiseResult);
@@ -186,7 +186,7 @@ export class WriteBuffer extends Disposable {
186186
* responsibility to slice hard work), but we can at least schedule a screen update as we
187187
* gain control.
188188
*/
189-
const continuation: (r: boolean) => void = (r: boolean) => Date.now() - startTime >= WRITE_TIMEOUT_MS
189+
const continuation: (r: boolean) => void = (r: boolean) => performance.now() - startTime >= WRITE_TIMEOUT_MS
190190
? setTimeout(() => this._innerWrite(0, r))
191191
: this._innerWrite(startTime, r);
192192

@@ -202,7 +202,8 @@ export class WriteBuffer extends Disposable {
202202
* throughput by eval'ing `startTime` upfront pulling at least one more chunk into the
203203
* current microtask queue (executed before setTimeout).
204204
*/
205-
// const continuation: (r: boolean) => void = Date.now() - startTime >= WRITE_TIMEOUT_MS
205+
// const continuation: (r: boolean) => void = performance.now() - startTime >=
206+
// WRITE_TIMEOUT_MS
206207
// ? r => setTimeout(() => this._innerWrite(0, r))
207208
// : r => this._innerWrite(startTime, r);
208209

@@ -222,7 +223,7 @@ export class WriteBuffer extends Disposable {
222223
this._bufferOffset++;
223224
this._pendingData -= data.length;
224225

225-
if (Date.now() - startTime >= WRITE_TIMEOUT_MS) {
226+
if (performance.now() - startTime >= WRITE_TIMEOUT_MS) {
226227
break;
227228
}
228229
}

0 commit comments

Comments
 (0)