Node.js vs C++ for mathematic

I have to write a server program that implements some fuzzy logic and I choose to write it in Node.js to take advantage of its event orientation. I have to work with difficult mathematic computational problem, and I don't know what's the best way to obtain performance:

  1. Write all in Node.js and use the power of the V8 engine for the mathematical task.
  2. Write a module in C++ that implements all the mathematical function and call it from Node.

Anyone that have experience in these type of computation on both platform?


Since you need the Node.js part anyway, go ahead, implement everything in Node.js. If it is fast enough, this is easy enough to maintain. It's very hard to predict the power of a virtual machine / JIT compiler.

If it is not fast enough, first think about algorithmic improvements. If this doesn't help and if profiling shows that the computation is the problem, go ahead, re-implement it in C++. But be aware that writing performant C++ code is not trivial. Make sure that you have a good profiler at hand and measure often.

In general I'd say C++ code is faster if written correctly. The tricky part is to write it correctly. Please check this article Google Paper on C++, Java, Scala, Go for more information. The gist is - managed languages make it a lot easier to write & maintain the code but if you need raw performance, C++ is the best. But it comes at the price of needing a lot of expertise and that the code is harder to maintain.

denshade, your C implementation goes only to 2e5 not 2e6, like you've done for js (linking to today's revs on Github):

Piping to /dev/null, and changing js also to 2e5, I get about 6.5 seconds for C and about 8.5 seconds for js (using some version of node) on my current computer.

Since your algorithm is O(n^2), I would expect 2e6 to take some 15 minutes, not 15 hours, but I haven't tried it. Maybe it does fall apart that bad for some reason.

(Note that I couldn't comment directly, since I'm brand new on SO and have no rep.)

it's pretty much impossible to answer this kind of question. The answer as always for these things is it depends on your skills and how much time and effort you are willing to put into it.

C++ always has the potential to be faster and more efficient as you have much closer control over all the things that matter. The downside it that you have to do all the things that matter and the generic implementations in the other language are probably done by someone who knows what they are doing and could well be better than a naive or quick implementation in c++

Plus often you'll find that the bottleneck isn't what you think it will be anyway, for example if reading in your data turns out to take 20 times as long as the calculations which isn't impossible then it hardly matters how fast the calculations are. And intuition about where the bottlenecks lie is often badly wrong even for experienced developers.

C++ uses the CPU and performs up to 10X faster than Node.js doing mathematical operations.

I've runned @denshade codes removing prints and the timing on 100000 numbers are exceptional:

  • 3 sec. for nodejs!

  • 6 sec. for gcc/clang compiled c

  • 6 sec. for hhvm ( php )

  • 14 sec for php7 w/ opcache

  • 15 sec for php7 w/o opcache

Nodejs is so fast because it's compiled and optimized over-time.

so, maybe you just need to test by your self which is the best language that fits your needs in this case.

One thing to consider with going the C++ route for complex mathematical computations is that you might be able to leverage an existing high performance library, such as BLAS, LAPACK, ARMA, etc.. where other developers have already put significant time and effort into providing highly optimized functionality. I doubt you'll find a similar level of high performance library for JavaScript. Certainly if you have an identified bottleneck around matrix calculations or linear algebra, one of these C++ libraries is the way to go.

If your calculations aren't trivial I'd like to issue a warning. JavaScript is very bad when you are going for heavy calculations. My story involves a simple prime program which you can find here:

Long story short. I created a simple, be it inefficient prime check function implemented in C & JavaScript. Both are implemented in the same way. The first 2000 000 primes are verified in 5 seconds in C. The same function in javascript lasted for more than 16 hours when run in node.js.

Following are the areas where Node.js is proving itself as a perfect technology partner.

● I/O bound Applications
● Data Streaming Applications
● Data Intensive Real-time Applications (DIRT)
● JSON APIs based Applications
● Single Page Applications

It is not advisable to use Node.js for CPU intensive applications.

here is API comparisons :

Need Your Help

Disable entire form elements with respect to a state. React

javascript html forms reactjs

I am disabling the inputs using the isFetching prop,

NSButton's images lose transparency on selection

cocoa nsbutton

I have edited some NSButtons in my Mac OS X application and set a custom image to them. The images are in png format with transparency. When I select a button, the background of the image goes whit...