project · February 08, 2023 · 2 min read
C++ Speed in Node.js (N-API)
A tech talk and template exploring how to call fast C++ from Node.js through N-API, so time-sensitive work runs at native speed while keeping Node's event loop.
C++ Speed in Node.js (N-API)
This started as a tech talk I wanted to give. I was running the same algorithm in C++ and in Node.js and could not get over the difference: the C++ version was blazing fast, and the Node version was significantly slower. That sent me down a rabbit hole, and I learned that you can write native add-ons and call C++ code straight from Node through N-API, which means you can hand the slow, time-sensitive parts to C++ and get native speed without leaving your Node app.
napi-js-cpp is the demo I built for the talk: the same algorithm in plain
JavaScript and in C++ via N-API, side by side, so you can actually see the gap.
Then I took the idea one step further. If Node is so good at handling lots of
connections with its event loop, what if you keep the server in Node but push the
heavy computation down to C++? express-napi is the result: an Express server
template wired up with N-API native add-ons, so you get the event-loop advantage
for serving requests and native speed for the parts that need it.
