I/O is slow. – The event loop – The node environment



I/O is slow. – The event loop – The node environment

0 0


what-is-node


On Github steeeve / what-is-node

node is a platform for creating server applications written in javascript

I/O is slow.

Most programs spend their time waiting for the network / disk. Threads and processes are an alternative, prcoesses don't scale well, threads are hard to program hence the term 'non-thread-safe', also use a lot of memory nginx and nodejs are event based.

network is 5x slower than disk

disk is 150000x slower than ram

ram is 15x slower than cpu

The event loop

what's the solution?

Code is single threaded

Callbacks for I/O

Spawn processes to do something harder

(messaging for the results)

The node environment

what's it like to code in node?

V8 engine Javascript (fast)

'requires' API, to keep your code modular

A bunch of libraries for I/O

NPM for package management

It's as simple as

// requiring a module, which we could have authored.
var http = require('http'); 

// Creating a server with a single request (event) handler.
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');

CLI tools

Nodejs handles disk and network in the same way.

forever

process supervisor

bower

asset management

http-server

one line http server

Bad for?

CRUD apps

CPU heavy apps

Good for?

e.g. lots of connections that do/get things e.g. logs of event streams!

APIs

Realtime

When you want to know wtf?! is going on

...oh hang on.

THE END