Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

waterfall much slower than series #621

Closed
elad opened this issue Sep 11, 2014 · 4 comments
Closed

waterfall much slower than series #621

elad opened this issue Sep 11, 2014 · 4 comments

Comments

@elad
Copy link

elad commented Sep 11, 2014

Hello,

I've put the following in a file called async.html and opened it in Chrome:

<html>
    <head>
        <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/async/0.9.0/async.js"></script>
        <script type="text/javascript">
            function log(msg) {
                console.log((new Date).toISOString(), msg);
            }

            log('waterfall: start');
            async.waterfall([
                function(callback) {
                    log('waterfall: function');
                    callback();
                }
            ], function(err) {
                log('waterfall: finish');
            });

            log('series: start');
            async.series([
                function(callback) {
                    log('series: function');
                    callback();
                }
            ], function(err) {
                log('series: finish');
            });
        </script>
    </head>
</html>

The output in the console looks like:

2014-09-11T14:16:41.136Z waterfall: start async.html:6
2014-09-11T14:16:41.141Z series: start async.html:6
2014-09-11T14:16:41.141Z series: function async.html:6
2014-09-11T14:16:41.142Z series: finish async.html:6
2014-09-11T14:16:41.150Z waterfall: function async.html:6
2014-09-11T14:16:41.151Z waterfall: finish async.html:6

Note the delay between waterfall's "start" and "function" (14ms) vs. series' (0ms). In a real-world web app I have the waterfall delay is usually around 600ms. (!)

Is this a known issue, or even an issue at all? is there a reason for such a delay?

@aearly
Copy link
Collaborator

aearly commented Sep 11, 2014

waterfall has a async.nextTick at each stage of its iteration to prevent stack overflows. In the browser, this is implemented as setTimeout(iterate, 0), which can actually take as long as 8 ms in certain cases.

If your functions are not actually asynchronous, i would not recommend using waterfall, bust something like _.compose instead.

@elad
Copy link
Author

elad commented Sep 11, 2014

Good call. I was under the impression it was something like that since it could not be reproduced in jsfiddle or node.js.

My functions are async but this suggests I should work around control flow (passing values) and use series if I don't want to suffer a performance impact.

I would suggest making this distinction clearer in the documentation, but I'll accept if this is something too obvious/esoteric. Feel free to close this issue.

Thanks! :)

@aearly
Copy link
Collaborator

aearly commented Sep 11, 2014

I actually would be a fan of removing the nextTick and just add the caveat that you could have a stack overflow if your functions aren't async. But that's something for @caolan to decide.

@BrandonZacharie
Copy link

It is important to use process.nextTick in asynchronous APIs. This is fully explained in the Node.js documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants