Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Make process.nextTick worlds faster for large queues.
Browse files Browse the repository at this point in the history
  • Loading branch information
creationix authored and ry committed Aug 20, 2010
1 parent 60b93cc commit 81a53e8
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/node.js
Expand Up @@ -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]();

This comment has been minimized.

Copy link
@bentomas

bentomas 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): http://gist.github.com/553061

This comment has been minimized.

Copy link
@creationix

creationix Aug 27, 2010

Author

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.

This comment has been minimized.

Copy link
@bentomas

bentomas Aug 28, 2010

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

}
nextTickQueue.splice(0, l);
};

process.nextTick = function (callback) {
Expand Down

0 comments on commit 81a53e8

Please sign in to comment.