0

I'm just starting out with Node.js and I understand that most operations must work with callbacks to be non-blocking. My question pertains to the methods that Underscore.js exposes. For example

_.shuffle([1, 2, 3, 4, 5, 6]);

Wouldn't this be considered synchronous code given that no callback is provided? Consider a large list to shuffle.

Trying to come to grips in terms of what libraries I can use with node without impacting the fundamentals of using node.

Thanks!

2
  • 1
    You should edit the question title to make it more clear what your actual question is. Personally, I see nothing wrong with synchronously performing CPU intensive tasks like sorting large arrays. Commented Jun 17, 2014 at 6:18
  • 1
    Note: having a callback doesn't mean it is asynchronous. Behind the scenes the function might only do some synchronous job and then invoking the callback. Try it! :-) Commented Jun 17, 2014 at 6:19

2 Answers 2

6

Node is single threaded so any work that needs to get done will eventually be done by that thread. The async nature of Node means that it always tries to keep itself busy with work instead of waiting around for data to be returned (things like database calls, networks calls, disk access, etc). When you read things about making sure code is asynchronous, these are the types of operations that people are talking about.

Shuffling a bunch of numbers is a bunch of work that has to be done by the single Node thread, making this type of call async wouldn't do anything. So yes, that call is synchronous and will block the thread but there really isn't an alternative (without spawning worker threads or additional node processes). This is one of the reasons that Node really isn't the best option if you have a lot of heavy computations to do since it will block the single thread. Node is best at doing lots and lots of short duration tasks quickly.

Note that shuffling a million numbers will probably still be faster than a single database call, so this particular operation wouldn't impact overall performance that much. If you need to shuffle 100 million numbers, Node probably isn't the right platform.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Bill. Now that I understand it almost makes my question a bit silly! Definitely cleared it up.
2

Yes, it's synchronous, but that's not a problem in this example (or really any of Underscore's methods).

The reason many node APIs are asynchronous is because they perform potentially long operations. In order to do so, the work is offloaded to native OS asynchronous facilities (sockets) or done on a separate thread. Only when the work is complete is the data marshaled back into JS land and a callback invoked.

In this case, you're dealing strictly with JavaScript-managed memory. Only one thread has access to JS memory; you can't share memory between threads. This means you must do your work (shuffling the array) synchronously.

Unless you're dealing with a truly large array, this won't be a problem.


The "never make synchronous calls in node" rule really applies to I/O and computationally expensive operations. That's why all the network, filesystem, crypto, and zlib APIs are asynchronous. You'll notice that other APIs like the URL/path parsing modules are synchronous calls.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.