10

I have a Node.js web app where I need to do some heavy computations on large matrices. Since Node.js is event driven, I anticipate that this will result in poor performance of my web app. What is the best way to handle CPU heavy tasks with Node?

Could I offload the computations to another server built in something like Python?

4
  • 1
    Note that Python is really not that efficient. Compared with C++ you might get a factor of 100 improvement in some cases, especially computation. Commented Mar 9, 2014 at 22:45
  • Right, if I went with this solution, Mu plan would be to implement he server in python! but do my matrix computations in NumPy/SciPy. Commented Mar 9, 2014 at 22:55
  • 1
    I was working on a physics simulation that was written in Python with SciPy and my task was to rewrite it in C++. After that I managed to get about x80 times speed improvement and 1000+ times accuracy improvement, thanks to C/C++'s long double. So I think it's worth it to write it in C++. Commented Mar 9, 2014 at 22:59
  • Ok, this make sense. Thanks for the help. Unfortunately, I do not know C++. Ideally I would like to use something I'm more familiar with like Python. Commented Mar 9, 2014 at 23:47

2 Answers 2

10

What you would really like to do in this case is an Addon.

Addons as described in the Node.js documentation are dynamically linked shared objects. They can provide glue to C and C++ libraries.

So you can write your heavy computation in a lower level language (C/C++) and do it much more efficiently than with JavaScript, no matter how powerful V8 is.

Read the docs on how to use Addons and I believe you'll find it a fantastic feature of NodeJS.

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

4 Comments

I think this is a better answer than mine, except possibly for the reason that I personally don't really know C/C++. The last time I had to do something similar the company already had all of the routines I needed built in Java... so it just seemed like a better idea to use exec.
I used NodeJS before with some additional stuff written in C++ and it worked great and was really fast too. Btw C++ is not that complicated. In fact though I started programming in high level languages right now I primarily use C++.
Right - I know C++ isn't terribly complicated (I did learn it in some of my comp-sci courses), but the problem is when you've got to tell your manager why you need to rewrite some code that's existed for five years and is very stable into a new language. That kind of conversation doesn't go over very well :D But, I'm still in agreement that this is a better way to go if you're able.
@Stephen, Why would you say that his answer is better? Even if you use an addon, the C++ code will still block node, wouldn't it? offloading the work to another server would make the process truly async. (I have no idea really, I'm just wondering)
1

Yes - you could offload to another server. Just use your normal request methods to post over the data you need to have 'computed'. Since requests are async, it won't block the normal flow of Node. When the request comes back, you've got the data you need and can do any last minute alterations before pumping it up to the client.

You could then have a 'server farm' that handles these things - for example on EC2 you can create a load balancer, and then just have node make the request to that balancer. No need to do anything fancy inside of node like remembering which server was the last one hit or anything like that.

Alternatively, if this was more of a command line thing and doesn't need to be terribly scalable, you can use node's exec to make a system call, then once again the callback will be called with whatever the return is.

Whatever you do, do not do those computations inside of node :) - your anticipations are correct. It'll bomb your performance, as I learned the hard way a couple of times. Async is your friend.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.