Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make process.nextTick worlds faster for large queues.

  • Loading branch information...
commit 81a53e83ab4815d84f065b8fd6c356931a48c695 1 parent 60b93cc
Tim Caswell creationix authored ry committed
Showing with 5 additions and 2 deletions.
  1. +5 −2 src/node.js
7 src/node.js
View
@@ -47,9 +47,12 @@ process.evalcx = function () {
var nextTickQueue = [];
process._tickCallback = function () {
- for (var l = nextTickQueue.length; l; l--) {
- nextTickQueue.shift()();
+ var l = nextTickQueue.length;
+ if (l === 0) return;
+ for (var i = 0; i < l; i++) {
+ nextTickQueue[i]();

There is a problem here if nextTickQueue[i] throws an error. The splice is never run and things get confused. The way to fix it is to catch errors, make sure things are spliced and then throw the errors on. Here's a patch to fix the problem (with an example which works after the patch is applied): http://gist.github.com/553061

Good catch, that will more closely follow the semantics from the original shift method. Is there a cost of having the try-catch inside the for loop? You could put the try..catch outside the loop and splice up to the index causing the error before throwing.

It's very important that this loop stays fast.

Good point about pulling the try/catch out. I've updated the gist and added a test: http://gist.github.com/553061

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
}
+ nextTickQueue.splice(0, l);
};
process.nextTick = function (callback) {
Benjamin Thomas

There is a problem here if nextTickQueue[i] throws an error. The splice is never run and things get confused. The way to fix it is to catch errors, make sure things are spliced and then throw the errors on. Here's a patch to fix the problem (with an example which works after the patch is applied): http://gist.github.com/553061

Tim Caswell

Good catch, that will more closely follow the semantics from the original shift method. Is there a cost of having the try-catch inside the for loop? You could put the try..catch outside the loop and splice up to the index causing the error before throwing.

It's very important that this loop stays fast.

Benjamin Thomas

Good point about pulling the try/catch out. I've updated the gist and added a test: http://gist.github.com/553061

Please sign in to comment.
Something went wrong with that request. Please try again.