Browse files

Make process.nextTick worlds faster for large queues.

  • Loading branch information...
1 parent 60b93cc commit 81a53e83ab4815d84f065b8fd6c356931a48c695 @creationix creationix committed with ry Aug 20, 2010
Showing with 5 additions and 2 deletions.
  1. +5 −2 src/node.js
7 src/node.js
@@ -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]();
bentomas added a line comment Aug 27, 2010

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):

creationix added a line comment Aug 27, 2010

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.

bentomas added a line comment Aug 28, 2010

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

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) {

0 comments on commit 81a53e8

Please sign in to comment.