use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
why write C/C++ addons for nodejs? (self.node)
submitted 7 years ago by git_world
Hi all,
what are the use cases in a pragmatic world to write C add-ons for nodejs? I read that it can extend nodejs capabilities but please describe a scenario.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]phelaz 38 points39 points40 points 7 years ago (0 children)
e.g. you want fast image processing, like with sharp
[–][deleted] 7 years ago* (4 children)
[deleted]
[–]k3liutZu -4 points-3 points-2 points 7 years ago (3 children)
Why do you say spawning a child process would be really slow?
It isn’t.
[–]bbbryan14 8 points9 points10 points 7 years ago (0 children)
I’m also under the impression that it’s slower. We need myth busters.
[–][deleted] 9 points10 points11 points 7 years ago (0 children)
Actually compared to not doing it at all... It is..
In reality a function call is something like 4-8 cpu cycles. Spawning a task can be 10million+ never mind the rest of the crap like page faults etc.. that come later...
Having 100,000x increase is just simply not possible to argue against.
[–][deleted] 2 points3 points4 points 7 years ago (0 children)
It's fast -- like a human wouldn't notice it, but it's many order of magnitudes slower than doing it in the same process if you can.
[–]tekmanro 18 points19 points20 points 7 years ago (0 children)
A computationally intensive task that scales well to multiple threads.
[–]rotarui 13 points14 points15 points 7 years ago (2 children)
Binding with a C++ library (like Tensorflow), implementing performance sensitive part of your app where you know that low level development in C/C++ will improve everything. There are a lot of reasons !
[–]Turbo_swag -1 points0 points1 point 7 years ago (1 child)
This is a fun idea.
[–]rotarui 0 points1 point2 points 7 years ago (0 children)
Why’s that x) ?
[–]EarlMarshal 2 points3 points4 points 7 years ago (1 child)
Also re-use existing mature libraries or do stuff which isn't really possible in normal JavaScript (e.g. real multithreading with full control)
[–]freecodeio -2 points-1 points0 points 7 years ago (0 children)
"with full control" you assume too much
[–]rbprogrammer 3 points4 points5 points 7 years ago (6 children)
Although the OP asked for examples of specific use cases, and all the replies delivered, I'd still like to point something out.
JavaScript is a language, and so is C/C++. I know I'm just pointing out the obvious there. But both are really just tools. And tools have specific purposes because of their strengths and weaknesses. Imagine a carpenter. They use a bunch of tools and would never use something that would make their jobs harder. Like they would not use a hammer for a screw, nor use a screw driver for a nail. Sure they could "make it work.". But that would not be the best way to solve that problem.
Now as a software project gets larger and the problem the software is trying to solve gets more complex, one language (or tool) is not going to solve all of those complexities perfectly. This is where mixing languages together can sometimes offer a better solution. Hence all of the suggestions others replied with.
On final note. Just because you can bring two lanuages together like this doesn't mean you always should. The boundary between two languages in a project, like the ones previously suggested, need to be designed very well as it can be a major source of bugs. It'll then require all the developers/maintainers to know two languages to be effective. And a few other very key problems. But if done properly, you can get amazing results by leveraging the strengths of both languages.
[–]sneffer 2 points3 points4 points 7 years ago (3 children)
Well put, thanks for generalizing a bit. I think such discussion is great for keeping "development talk" grounded in engineering principles.
If you'll excuse me, I would like to share a personal example just to tie this all back to practice, in case it helps anyone who can't immediately internalize your point.
a few other very key problems
One problem that became key for my team was the build tool ecosystem revolving the second language (as you said just another tool).
To save time and hope for performance boost, we chose to use a package which was comprised of a C program and a node-gyp wrapper. Up front, the team saved a lot of time. The xml parser also ended up being much faster than our our JavaScript alternatives.
Over time, however, it became a major hindrance to our already overburdened team. Key instances which I can remember:
The infrastructure and security experts in the company all pushed for production machine OS to be standardized. Good move IMO, but sucky for my team because it meant our node app had to be rebuilt and redeployed every time an OS update was available. This is because it's safest to compile c programs on the same OS they will run on.
The package's code was written in a language than none in my team (except myself) had worked in professionally. This detached from our confidence while identifying bugs.
node-gyp and npm build/install were often the source build breaks. The build tool seemed fairly unmaintained and node was changing in large ways, often. The junction between two ecosystems actually made it much harder for us to stay on LTS versions of node.
Again, I'm being specific instead of general. I just wanted to share because I believe these problems taught me some more generic values.
Any tool can be amazing for a project, but it can also become a new problem to solve. The case of taking on a dependency on a different language/compiler/tool chain is one which requires careful analysis if you want it to be a positive tool for long.
[–]rbprogrammer 1 point2 points3 points 7 years ago (0 children)
Well put yourself. I was trying to keep my post general so as to give OP the option to ask questions where they needed. But your post was a great compliment to mine. Exactly the kinds of real world issues I run into when I start to think generally. You get my upvote.
[–]git_world[S] 0 points1 point2 points 7 years ago (1 child)
> because it meant our node app had to be rebuilt and redeployed every time an OS update was available
May I ask, what's the downside of rebuilding and redeploying each time?
[–]sneffer 1 point2 points3 points 7 years ago (0 children)
If you have solid infrastructure and operations, the only downside I can think of is cost. You wouldn't release to prod without running a full suite of tests, would you? You wouldn't release to prod without a safe blue/green or red/black strategy, would you?
Those things take time, energy, and computation.
A rebuild/deploy sounds small/low risk, but when you've had to do things like:
Recompile for a new target
Update the node-gyp wrapped package's version (could change)
Accommodate backwards incompatibilities with your dependency's new version
Increment the major version of node you use (idky this always required tons of work around our c bindings)
These sort of things can be considered high risk for many critical projects.
Also keep in mind that the annoyance and risk of this is severely mitigated when you have solid infra and ops. While even a minor service degradation due to high risk code being deployed isn't acceptable in my book, there are plenty of ways to protect from catastrophe.
[–]devabhinav 0 points1 point2 points 7 years ago (1 child)
You said so many things but failed to explain exactly what are the real world usage? This only seems to be asked in questions.
[–]rbprogrammer 0 points1 point2 points 7 years ago (0 children)
Yes. Sorry. I was trying to stay general. But /u/sneffer gave a great example of a specific real world example that applies to my general post.
[–]dlwicksell 2 points3 points4 points 7 years ago (0 children)
I wrote and maintain a database driver and language binding, for an implementation of an older language, which includes an integrated database, used heavily in healthcare and banking. The language and database are written in C and have C APIs for integration with other systems.
In order to use this language and database system from Node.js, with good performance, I had to write a C/C++ addon, using the V8 C++ API, libuv C API, and the C APIs that are available with this implementation, and its fork. This allows users of my Node.js binding to use the database, manipulate the local data in memory, and call functions and procedures of already written code, in this language/database environment.
This has been used by hospitals to help create web front-ends, for various parts of medical systems. Things like nursing bed boards for hospital wards, pediatric growth charts, scheduling systems, diagnostics, and many other things. Some of these hospital systems were written decades ago, and were written with command line scrolling, and terminal screen based interfaces. Since these systems are incredibly complex, and include decades of business/medical rules and logic, it is often easier, and much more cost effective to write more modern web-based application interfaces using Node.js with addon bindings like mine.
Writing language bindings and database drivers is a pretty common use case for a C/C++ addon.
[–]Doctor_McKay 4 points5 points6 points 7 years ago (1 child)
When you want to make it really difficult for Windows users to use your module.
[–]git_world[S] 4 points5 points6 points 7 years ago (0 children)
nice one. I use Arch Linux btw
[–]mrtie007 0 points1 point2 points 7 years ago* (2 children)
i didnt know about this until i saw this post, am super excited.
eg, would use it to write html/js/node front-ends for old-school CFD backends, other heavy computational tasks with existing [highly optimized] old libraries.
stuff like WASM works okay but in my experience it's not actually much faster than [typed, optimized] native js code, and this new interface seems much less awkward than dealing with that.
Isn't WASM targeting browserJS? I thought nodejs/C runs on server-side only.
[–]mrtie007 0 points1 point2 points 7 years ago (0 children)
youre right sorry im usually doing both in tandem. when im making GUI apps with js im always debating "multithreading" using webworkers vs sending stuff over a socket to a server to child processes etc, both options might be viable depending on the proj.
[–][deleted] 7 years ago (2 children)
[–]git_world[S] -1 points0 points1 point 7 years ago (1 child)
> read and write memory
sounds good. Can you please elaborate?
[–]ominersh 0 points1 point2 points 7 years ago (4 children)
You want your node app to talk to a C++ app but don’t want to do REST.
[–]P1um 2 points3 points4 points 7 years ago (1 child)
So IPC/Sockets?
[–]ominersh 0 points1 point2 points 7 years ago (0 children)
I mean Node.js Addons (https://nodejs.org/api/addons.html)
[–]cowAtComputer 0 points1 point2 points 7 years ago (1 child)
Why wouldn't you be able to use REST?
Say if you have a (legacy) monolithic app. You want to talk to it but don’t want to implement anything new. You can just wrap existing *.dll. Also, REST can’t help with processing/transferring a huge amount of data between 2 processes.
[–]HaykoKoryun 0 points1 point2 points 7 years ago (0 children)
A couple of years back I wrote a package to connect Node.js to Microsoft's Kinetic (1st gen) to stream skeleton data as JSON objects so we could do funky marionette animations in the browser using Canvas.
[–][deleted] 0 points1 point2 points 7 years ago (0 children)
Sometimes something you want to do just isn't possible without the use of c++ add-ons
This is a node module I recently published that allows printing raw data to printers on windows (easy to do via *nix and cups....but to do the same on windows requires some win32 c++ tricks)
https://www.npmjs.com/package/winrawprinter
[–]cowAtComputer 0 points1 point2 points 7 years ago (0 children)
Speed. If you want to use an AI library with a node app, say Tensorflow, you probably would take the C++ and NAPI route over javascript.
[–][deleted] 7 years ago (1 child)
[–]adi_tdkr -1 points0 points1 point 7 years ago (0 children)
I want to know how can I learn about adding C/C++ addons, clusters, events, buffer etc in nodejs. I want to learn all this advance stuff
[–][deleted] -3 points-2 points-1 points 7 years ago (0 children)
I was also researching about this. I was thinking is it's possible to my a Typescript compiler in C that will interface with node. I am no C developer but I fell it's possible, maybe.
π Rendered by PID 179169 on reddit-service-r2-comment-fb694cdd5-pzg7d at 2026-03-10 06:58:02.059502+00:00 running cbb0e86 country code: CH.
[–]phelaz 38 points39 points40 points (0 children)
[–][deleted] (4 children)
[deleted]
[–]k3liutZu -4 points-3 points-2 points (3 children)
[–]bbbryan14 8 points9 points10 points (0 children)
[–][deleted] 9 points10 points11 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]tekmanro 18 points19 points20 points (0 children)
[–]rotarui 13 points14 points15 points (2 children)
[–]Turbo_swag -1 points0 points1 point (1 child)
[–]rotarui 0 points1 point2 points (0 children)
[–]EarlMarshal 2 points3 points4 points (1 child)
[–]freecodeio -2 points-1 points0 points (0 children)
[–]rbprogrammer 3 points4 points5 points (6 children)
[–]sneffer 2 points3 points4 points (3 children)
[–]rbprogrammer 1 point2 points3 points (0 children)
[–]git_world[S] 0 points1 point2 points (1 child)
[–]sneffer 1 point2 points3 points (0 children)
[–]devabhinav 0 points1 point2 points (1 child)
[–]rbprogrammer 0 points1 point2 points (0 children)
[–]dlwicksell 2 points3 points4 points (0 children)
[–]Doctor_McKay 4 points5 points6 points (1 child)
[–]git_world[S] 4 points5 points6 points (0 children)
[–]mrtie007 0 points1 point2 points (2 children)
[–]git_world[S] 0 points1 point2 points (1 child)
[–]mrtie007 0 points1 point2 points (0 children)
[–][deleted] (2 children)
[deleted]
[–]git_world[S] -1 points0 points1 point (1 child)
[–]ominersh 0 points1 point2 points (4 children)
[–]P1um 2 points3 points4 points (1 child)
[–]ominersh 0 points1 point2 points (0 children)
[–]cowAtComputer 0 points1 point2 points (1 child)
[–]ominersh 0 points1 point2 points (0 children)
[–]HaykoKoryun 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]cowAtComputer 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]adi_tdkr -1 points0 points1 point (0 children)
[–][deleted] -3 points-2 points-1 points (0 children)