6

Suppose I have a simple Hello, World! file in C++ or C (whatever will help me use it easier in Node.js, preferably C) and want to run it from a Node.js file. What is the most efficient way considering that the file will be used to boost the performance (changing CPU intensive functions from Node.js to C/C++)?

I came across the addons, but it seems to me, that in order to use it, I'll have to convert a lot of code to bring it to that format. Is there an easier way?

4
  • 2
    I appreciate the sentiment, but regardless, in my current scenario, trying to boost node.js with c or c++ it doesn't really help me. I understand what you are saying and I came from a c background, probably like you but found a project where node.js was useful and wanted to test it. I don't think its bad to try to improve its performance, I would appreciate it even more if instead of just down-voting me without saying anything, at least explain what you don't understand about the question. edit: I guess the comment was deleted.
    – John James
    Mar 4, 2018 at 21:03
  • The answer is subjective - there are different ways of integrating native code, from calling external executables to writing native-code extensions to node that you load as modules. You will have to figure out what "efficient" means to you and why using native JS wouldn't work for you.
    – Joe
    Mar 4, 2018 at 21:03
  • @JohnJames The downvote, is because you didn't post anything useful to help answer. Also, I understand that you need to do it, I just had to say it. My comment was going to be deleted, I just didn't know that so quickly. I like that, it keeps SO clean from people who are frustrated for the success of horrible technologies like myself. That said, it's highly likely that there is a way to call c functions from whithin node.js. Note that being lower level, many of node.js is probably written in c. Mar 4, 2018 at 21:06
  • There might be something that you can google about it, like "extending node.js with c" or "calling c functions from node.js". Mar 4, 2018 at 21:07

3 Answers 3

25

I don't see why using child_process would be slower than other options.

I recommend:

// myCFile.c
#include <stdio.h>

int main(void){

    // Processor-intensive computations
    int x = 1+2+3;

    // Send to node via standard output
    printf("%d", x);

    // Terminate the process
    return 0;
}

Compile:

gcc -o myExecutable myCFile.c

And use child_process like this:

// File myNodeFile.js
const { exec } = require("child_process");
exec("./myExecutable", (error, stdout, stderr) => console.log(stdout));
8

For our image segmentation algorithm that I had written in C++, I needed to help the full-stack developer wrap the shared library for Node.js. As far as I can see, from a day of googling around and hacking into Node.js, which is a somewhat unfamiliar world for me, that there are two major options:

  1. using node-ffi, or,
  2. addons as you have already stated.

For 1. above, you do not need to do much. You simply need to require the ffi, ref and ref-array packages/addons in node.js to be able to call the C API of your application code. There is some nice tutorial that I followed, which helped me get going in 15 minutes.

However, I needed to choose 2. above for our project in the end. This was due to the fact that our full-stack developer was relying on some other addons that needed the latest version of Node.js. Apparently, when we check the issue board of node-ffi, as of this answer's posting time, it does not support the v9.x family of node.js. Hence, I went the native addons way. It has taken me roughly 4 hours to understand and write the code. I am not sure if it is the most convenient/efficient way possible, but what I did was to

  1. use buffers to allocate memory in Node.js,
  2. write a simple addon using nan in Node.js that reinterpret_casts the char* buffer of Node.js and calls the very same C API of our shared library, and finally,
  3. link against the shared library we had created using binding.gyp.

Apparently, Native Abstractions for Node.js (aka nan) is supposed to be used by users to avoid the need to handle breaking changes introduced in v8. There is another nice tutorial I have found, which helped me solve my problem easily.

Finally, Scott Frees' blog site seems to have a lot of self-contained articles/examples for those who would like to go deeper. He also argues in which situations you should be preferring one approach over the other (node-ffi over native addons, for instance). Basically, what I understand is that writing native addons will be more efficient, even though for our application it did not matter much. node-ffi gives satisfactory behaviour, too, as we were solving an image segmentation problem (which anyways takes more time than the call overhead).

So, in short,

I came across the addons but it seems to me, that in order to use it I'll have to convert a lot of code to bring it to that format.

Well, not necessarily! It depends on what you are willing to achieve. It can be as easy as compiling your C++ code for a specific C-API shared library, and then writing a 20-liner wrapper in nan, which basically does some reinterpret_cast for in-place memory operations, and finally linking against the library in binding.gyp.

Is there an easier way?

Yes, there is. node-ffi can help you solve the problem under half an hour. But then, it might not be the most efficient for your scenario, or it might not be a viable option for you, as it currently does not build with the v9.x family of Node.js.

1
  • The articles are really useful. I must admit I saw the Addon and the node-ffi before but the latter seemed like it compromised efficient for convenience and the former the opposite. After seeing your post and your articles though, it made me decide which one to use, and help go on my way, thank you.
    – John James
    Mar 5, 2018 at 19:05
1

There is an option to compile C/C++ with emscripten to WebAssembly and for quick execution on Node. Calling WebAssembly code from JavaScript is not trivial, but allows more flexibility for input and output parameters than communicating with child process.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.