Using Common Node to build StartHQ

Post on 13-Jan-2015

608 views 1 download

description

A HelsinkiJS talk by Alex from https://starthq.com The video of this talk is available at http://youtu.be/pmyDJnEza6A

transcript

Server-Side JS @ StartHQNode, Fibers, Common Node etc.

Fibers

● lightweight threads

● co-operative multitasking○ still using one event loop

● individual stack, shared heap○ require a bit of additional memory for stacks

● similar to generators (in ES6)○ but no need to mark each yielding method

Fibers usage example

function sleep(ms) { var fiber = Fiber.current; setTimeout(function() { fiber.run(); }, ms); Fiber.yield();}

Sleep: Async. vs. Sync.

console.log('before');setTimeout(function() { console.log('after 1s');}, 1000);

console.log('before');sleep(1000);console.log('after 1s');

CommonJS

● Set of specifications○ Modules, Packages, Unit Testing

○ Binary, IO, Filesystem, JSGI, etc.

● Implemented by RingoJS and others○ Ringo runs on top of the JVM

● Node supports the first 3, but not others

because they assume sync. I/O

Common Node

● Implements CommonJS specs using Fibers

on top of Node

● Allows for code reuse with RingoJS

● Simplifies business logic & debugging○ supports for, exceptions etc. for flow control

● Performance: 80%-120% of Node○ Uses a bit more memory e.g. 28MB vs. 32MB

Benchmarks

Node Streams

var body = [];stream.on('data', function (chunk) { body.push(chunk);})stream.on('end', function () { callback(body);});

Node Streams2

var body = [];stream.on('readable', function () { body.push(stream.read());})stream.on('end', function () { callback(body);});

Common Node Streams

var body = [], chunk;while(chunk = stream.read(null)) { body.push(chunk);}return body;

OR

return stream.read();

Ecosystem

● Ringo modules

● Common Utils

● Mongo Sync

● Stick

● Wrapping Node packages

Common Node at StartHQ

● Use it for everything○ API, static resources

○ background processing

○ command line tools

● Extractors API

● Sentry stack traces

Pitfalls

● Trying to yield or resume outside fiber

● Using async libs with fibers, such as Express

● Writing to global variables

● Separate fiber instance per dependency

● Unreliable third party packages○ Throws into event loop & crashed program

GitHub Links

● laverdet/node-fibers

● olegp/common-node

● olegp/notes

● starthq/extractor

Thank you!@alexlamsl