Hacking Node for fun and profit

Steve Venzerul

What is JavaScript?

This seems almost like a rhetorical question, the answer to which is - it's a language, of course. But it's not so simple.

Many JS programmers confuse the language with libraries (I program in jQuery), with runtimes (I'm a Node programmer), and cannot tell which APIs belong to which environment.

 

Try answering the following:

    1. Which API does Math.random() belong to? Where is it specified?

    2. What about XMLHttpRequest?

    3. setTimeout(), setInterval() and friends?

    4. and fs.readFileSync() ?

    5. console.log()/error()/time()/table()?

What is Node.js?

Node is a runtime for Javascript code which uses V8 as the engine, and layers various libraries and bindings on top, like libuv, zlib, openSSL and others. Node provides the essential glue and support to make a lot more out of V8 than just an engine to execute Javascript.

What is V8?

V8 is a Javascript engine produced by Google. It currently implements around 98% of the ES2015 spec, with the notable exception of modules. It is a highly optimized interpreter, JIT compiler and profiler. On it's own, it does not provide anything more than JS execution, so no DOM functions, no HTTP servers, no filesystem access. Nothing except the runtime libraries specified by the ES6 spec.

A little bit about libuv

libuv enforces an asynchronous, event-driven style of programming. Its core job is to provide an event loop and callback based notifications of I/O and other activities. libuv offers core utilities like timers, non-blocking networking support, asynchronous file system access, child processes and more.

Using libuv's own introduction:

Practically all of the useful utilities, libraries and functionality provided by Node is, at it's core, driven by libuv's event loop. It is the thing that provides asynchronicity to operations that are traditionally synchronous, or 'blocking'.

 

To learn more about libuv and it's features, you can check out this intro by none other than Bert Belder, co-founder of strongloop and a Node core commiter. 

https://nikhilm.github.io/uvbook/basics.html

The Event Loop - Brought to you by libuv

image credit: http://stackoverflow.com/questions/21596172/what-function-gets-put-into-eventloop-in-nodejs-and-js

Node.js Architecture

Image credit: https://arenli.com/architecture-of-node-js-internal-codebase-57cd8376b71f#.zfrav8cpr

Node.js has tons of JS

Speed in a convenient package - Native Addons

Things you need:

    1. A use case -- No point in going this deep if regular JS will do

    2. A little knowledge of C/C++ -- there's no escaping this one

    3. Patience in getting the bindings.gyp file to work (least favourite           part)

    4. Xcode and friends (command line tools)

    5. Preferably NAN (unless you wanna rewrite and recompile your           C/C++ for every breaking release of V8)

    6. Performance benchmarking (you wanna be really, really, really           sure that your problem is JS and not poor algorithm choice​. 

        Can't stress this step enough.

node-gyp - Not bad, but still sucks

A word about performance

There's a long standing and pervasive myth that JS is slow, that interpreted languages are slow, blah blah blah.

The first question is slow in doing what? Yes, some things are definitely faster when written using compiled languages like C/C++ however, this comes at a cost of productivity.

If all we wanted was brute speed, we'd just all be writing Assembly and be done with it. But we all know that's not the case by a wide margin. 

Balancing the equation of productivity vs. raw speed is the toughest part.

That said, sometimes there's a place and time where going down closer to the metal is the only way to get something working in the buttery smooth way we'd like it to be. YMMV.

x4.241 increase in speed

Results

Happy Holidays and Happy Hacking

(HHHH)

Hacking Node for fun and profit

By signupskm

Hacking Node for fun and profit

  • 1,200