Short article

NodeJS and C++ represent different extremes for “how to do sums”. For some situations, one of these is clearly the correct option. Your business problem may include situations for both tools, so you want to join them together. The business case, IMO:

  • A quick/cheap UI/UX, developed in slow NodeJS, with a lot of attention on UX
  • A slow dev/ fast execution C++ binary that does heavy number crunching

Fun Fact: in 2016, I made a “addon API” in Node, which used carefully stacking of callbacks, so nothing blocked; and performed if statements, and Array.push about 3000 times to build a tree of available addons (i.e. if you book at 09:30, you could have X1,Y1 or Z1, if you book at 10:00, X1 and Z1 are available, if you book at 10:30 Y2 is good). This took about 3000ms to execute. It wasn't blocked on the DB (that was ~1000 rows in each collection) as the queries where quite simple. There was no forking/shell-exec/exec involved.
I think when node people say “node is reeaally fast” they mean “I can make a simple webservice in 2h”. I understand more recent NodeJS is abit faster as they adopted libUV 1

There are quite a few ways to do this (which to me; implies other developers also think that NodeJS isn't soo fast that there is no value in linking a simpler/ closer-to-asm language to it).

Available options

  • Use FFI 1 git FFI 2 is a common way to link C to other languages. FFI itself is MIT licensed code. There are moves to make this a core feature of NodeJS 3 so it wouldn't need to be installed as an extra.
  • Build a N-API 4 (a C++ wrapper layer) with git some walk-throughs 5 6 7 8 9. The last link is also using FFI in addition to an n-api. N-API can support Promises made in C++ 10.
  • Use GYP ~ the same name as the python-C bridge git 11 walkthrough 12. The wiki page says that the founder of GYP ~ google ~ has replaced it with GN 13, which is similar but faster/better docs. NB: As google seem not to use MSFT internally, this approach has weak support for MSFT 14.
  • Use webasm git 15 16 17 18. This project is to compile raw source code of a language into JS, so it can be run inside a browser. People have used this to run Quake1 19 from id software inside a browser.

Some similar articles in engineering