Prefer performance.now() over Date.now()
performance.now() is faster than Date.now() as there's no need to consult the system clock. The more important part of this is that performance.now() is monotonic and is guaranteed to never to backwards.
This commit is contained in:
@@ -74,14 +74,14 @@ abstract class TaskQueue implements ITaskQueue {
|
||||
let lastDeadlineRemaining = deadline.timeRemaining();
|
||||
let deadlineRemaining = 0;
|
||||
while (this._i < this._tasks.length) {
|
||||
taskDuration = Date.now();
|
||||
taskDuration = performance.now();
|
||||
if (!this._tasks[this._i]()) {
|
||||
this._i++;
|
||||
}
|
||||
// other than performance.now, Date.now might not be stable (changes on wall clock changes),
|
||||
// this is not an issue here as a clock change during a short running task is very unlikely
|
||||
// in case it still happened and leads to negative duration, simply assume 1 msec
|
||||
taskDuration = Math.max(1, Date.now() - taskDuration);
|
||||
// other than performance.now, performance.now might not be stable (changes on wall clock
|
||||
// changes), this is not an issue here as a clock change during a short running task is very
|
||||
// unlikely in case it still happened and leads to negative duration, simply assume 1 msec
|
||||
taskDuration = Math.max(1, performance.now() - taskDuration);
|
||||
longestTask = Math.max(taskDuration, longestTask);
|
||||
// Guess the following task will take a similar time to the longest task in this batch, allow
|
||||
// additional room to try avoid exceeding the deadline
|
||||
@@ -116,9 +116,9 @@ export class PriorityTaskQueue extends TaskQueue {
|
||||
}
|
||||
|
||||
private _createDeadline(duration: number): ITaskDeadline {
|
||||
const end = Date.now() + duration;
|
||||
const end = performance.now() + duration;
|
||||
return {
|
||||
timeRemaining: () => Math.max(0, end - Date.now())
|
||||
timeRemaining: () => Math.max(0, end - performance.now())
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user