node.js Native Extensions
or, what to do when JS is not good enough
Currently helping Chaos Group build a cloud rendering platform
Competitive programming enthusiast
- So, we'll talk about performance
- ...just to keep the stereotype alive
So, you need more speed?
V8 is really fast
but it can also be kinda slow
- Abstractions, GC, dynamic typing, etc. all come at a price
We'll assume that, for some reason, our users need a way to sort a large amount of numbers
A typical CommonJS module
A basic C++ extension module
Build with node-gyp
- npm install -g node-gyp
- node-gyp configure build
- results are by default in build/Release/algorithm.node
The sort function
- runs in less than a second - that's much better
Are we done?
- Would you run this in production?
- Our Sort function is not very asynchronous.
Reasons to avoid native extensions?
Bad for portability
- C++ is not as portable as advertised
Harder to maintain
- Adds one more language to the codebase
- It's ugly
- You hate C++ (just guessing)
When to avoid going the C++ way
- When you don't have to (duh!)
- IO-bound code
- Huge number of small function calls