Speed up your NodeJS app with N-API and Linux epoll

The JavaScript’s popularity and versatility are huge and growing. The NodeJS-NPM ecosystem is nice and powerful. And the N-API part of NodeJS allows all kinds of communication between the C/C++ addons and the JavaScript side of your app. If you write the crucial parts of your app in C, you can speed it up significantly. For example, you can add producer-consumer threads to it and make better use of your machine’s CPUs.

If your server runs Linux, you can improve the scalability of your app and further speed it up by using Linux epoll and eventfd features. It is nice to have event-driven support on a kernel level and build competitive real-time apps for financial, cryptocurrency and other sectors where speed is paramount.

This project is an example of such an app. On its JavaScript side (file b3.js), it has module B3 which creates two instances of module B2 to support bidirectionality. The B2 module (files b2/*.*) is implemented in C and exposes to the JavaScript side its Producer and Consumer objects, among other properties. The Producer and Consumer objects run on two separate threads and exchange Tokens using their Bounded Buffer. Different Producer/Consumer implementations can be requested from the JavaScript side. One of the available Consumer implementations calls the JavaScript-side function (which runs on the main thread) each time a token is consumed, and then waits until the JavaScript side is done with the token.

I would like to extend my sincere gratitude and appreciation to Gabriel Schulhof for his wonderful example of a thread-safe function roundtrip — that example made this project possible. Thanks again, Gabriel!


Originally published at github.com.