4 ms·
> This can be extremely important if you have a project with heavy CPU-use Would you recommend using something different than JavaScript when writing CPU heavy
by Kiro 12y ago
> This can be extremely important if you have a project with heavy CPU-use
Would you recommend using something different than JavaScript when writing CPU heavy apps? I was under the impression that it's better suited when dealing with high I/O.
- FooBarWidget 12y agoTechnically you can offload CPU-heavy things to a cluster of extra Node processes that handle work in a serial fashion. Nothing wrong with this; if you're comfortable with Javascript then this is probably better than rewriting those parts in different languages.
- lsiebert 12y agoYou could, and that might get you the performance you need, but if not I would likely use https://github.com/node-ffi/node-ffi https://github.com/node-ffi/node-ffi or maybe http://www.swig.org/ http://www.swig.org/ to call out to C++ or C code. There's probably also a way to call Java, ( a quick search suggests https://github.com/joeferner/node-java https://github.com/joeferner/node-java perhaps) but I can't speak to that in particular.
- mschoebel 12y agoIMO if you really need to do heavy-duty processing, C/C++ will be fastest. On the other hand Node will make development easier. You have to judge for yourself which will be a better use of resources. Having more costs upfront for programming, or more costs in the long-run for servers.
- romanovcode 12y agoI would not recommend using Javascript at all when using CPU heavy apps. C++, Java, C# would be much better.
- Kiro 12y agoSorry, that's what I meant. Updated my question. Thanks.
- bananaoomarang 12y agoGenerally speaking for CPU bound tasks you should use something like edge.js (http://tjanczuk.github.io/edge/#/ http://tjanczuk.github.io/edge/#/) if you're using Node for dev speed/ease's sake.
- askmike 12y agoIf you decide to offload some CPU heavy parts I would personally use a messaging protocol like zeroMQ to keep a loose coupling.
- dagw 12y agoSure if your project is almost exclusively CPU intensive then use something else. But sometimes you have a project that already is in JavaScript and where JavaScript mostly makes sense, but you have one or two task that are small but fairly CPU intensive. Then knowing how to write fast JavaScript is a pretty good idea since JavaScript can be pretty fast these days and the overhead (both in terms of runtime and developer time) of calling out to second language isn't always trivial and it's nice if you can avoid it. Secondly (and not entirely relevant to this) people are doing more and more 'clever' CPU-intensive things client side these days where JavaScript is your only choice. So understanding the performance characteristics of different JavaScript implementations can definitely come in handy there.
- richmarr 12y agoA few folks have replied with the usual "use C/C++/Java instead", but in the real world it's often either impractical (or rather commercially indefensible) to fork out a different environment with its own training, testing, environment, automation, documentation and maintenance overheads. A blanket rejection of Node for CPU-heavy tasks is naive. On the issue of performance, V8 lets Javascript run pretty quickly. Yes, there are languages that broadly offer faster execution, but that's far from the only factor in choosing a solution. The main issue from my perspective is that the event loop can easily get blocked by CPU-bound tasks, preventing it from doing other things, e.g. responding to HTTP requests. You hit a similar problem with a Java servlet runner, eg. if a couple of your threads are bogged down on CPU-heavy tasks then they can't be responding to requests. My personal preference would be to split CPU-heavy operations out so that they happen elsewhere, regardless of language, e.g having large PDFs generated by an internal microservice rather than by the webserver, or maybe via a queue in some cases. But that's just a personal preference.
- k__ 12y agoReally? Companies I worked for always had two environments, one for new features and one for performance. Like PHP and C, new features where implemented in PHP and if they caught on, they got reimplemented in C if they needed better performance.
- richmarr 12y agoSure, if the companies you've worked for are in a field that needs incremental performance gains and are willing to pay for it then that's totally rational. Typically I see client-side performance concerns outweighing server-side performance in a ratio of 70/30 or so, with the remaining server-side performance biased towards I/O concerns like waiting for data, or file system reads with a ratio of 90/10 or more. That puts the actual saving available to language or algorithm changes in the app layer to be less than 3% for the kinds of apps I've worked on. I usually work at companies who are starting out, looking for Product Market Fit, where those marginal gains aren't worth the cost of reimplementing.
- 12y ago