Mr Cold
Mr Cold

Reputation: 1573

Should I use underscore function in node.js?

I have 3 tasks running parallel. In the first task, I use underscore method to loop through an array(for example _.each ). Does that underscore method block the other 2 tasks? If so, isn't it a bad idea to use underscore module in node.js?

Upvotes: 0

Views: 411

Answers (1)

Chris Tavares
Chris Tavares

Reputation: 30411

Yes, it blocks. No, it has nothing to do with using underscore.

Javascript is single threaded. When you register a bunch of event handlers or setTimeouts, they do NOT run in parallel - each one runs until it gets back to the event loop, then another one is pulled off the event queue and executed.

If your array is huge and you need to do a bunch of synchronous work on each element, you'll probably want to split up the work across multiple turns of the event loop. However, if it's reasonably small then it probably won't matter.

This has nothing to do with underscore, this is just the nature of Javascript.

Upvotes: 2

Related Questions