Skip to content

Commit

Permalink
perf(index): remove capacity defined check
Browse files Browse the repository at this point in the history
..and duplicate/unnecessary queue reset
  • Loading branch information
EisenbergEffect committed Oct 1, 2017
1 parent 613e877 commit ef6c4b8
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class TaskQueue {
// grow, but to avoid an O(n) walk for every MicroTask we execute, we don't
// shift MicroTasks off the queue after they have been executed.
// Instead, we periodically shift 1024 MicroTasks off the queue.
if (capacity && index > capacity) {
if (index > capacity) {
// Manually shift all values starting at the index back to the
// beginning of the queue.
for (let scan = 0, newLength = queue.length - index; scan < newLength; scan++) {
Expand All @@ -135,8 +135,6 @@ export class TaskQueue {
} finally {
this.flushing = false;
}

queue.length = 0;
}

/**
Expand All @@ -151,6 +149,7 @@ export class TaskQueue {
if (this.longStacks) {
task.stack = this.prepareQueueStack(microStackSeparator);
}

this.microTaskQueue.push(task);
}

Expand All @@ -166,6 +165,7 @@ export class TaskQueue {
if (this.longStacks) {
task.stack = this.prepareQueueStack(stackSeparator);
}

this.taskQueue.push(task);
}

Expand All @@ -175,33 +175,32 @@ export class TaskQueue {
flushTaskQueue(): void {
let queue = this.taskQueue;
this.taskQueue = []; //recursive calls to queueTask should be scheduled after the next cycle

this._flushQueue(queue);
this._flushQueue(queue, Number.MAX_VALUE);
}

/**
* Immediately flushes the micro task queue.
*/
flushMicroTaskQueue(): void {
let queue = this.microTaskQueue;
let capacity = this.microTaskQueueCapacity;

this._flushQueue(queue, capacity);

this._flushQueue(queue, this.microTaskQueueCapacity);
queue.length = 0;
}

prepareQueueStack(separator) {
let stack = separator + filterQueueStack(captureStack());

if (typeof this.stack === 'string') {
stack = filterFlushStack(stack) + this.stack;
}

return stack;
}
}

function captureStack() {
let error = new Error();

// Firefox, Chrome, Edge all have .stack defined by now, IE has not.
if (error.stack) {
return error.stack;
Expand All @@ -222,13 +221,16 @@ function filterQueueStack(stack) {
function filterFlushStack(stack) {
// Remove bottom frames starting with the last flushTaskQueue or flushMicroTaskQueue
let index = stack.lastIndexOf('flushMicroTaskQueue');

if (index < 0) {
index = stack.lastIndexOf('flushTaskQueue');
if (index < 0) {
return stack;
}
}

index = stack.lastIndexOf('\n', index);

return index < 0 ? stack : stack.substr(0, index);
// The following would work but without regex support to match from end of string,
// it's hard to ensure we have the last occurence of "flushTaskQueue".
Expand Down

0 comments on commit ef6c4b8

Please sign in to comment.