Using Common Node to build StartHQ

1,178 views

Published on

A HelsinkiJS talk by Alex from https://starthq.com

The video of this talk is available at http://youtu.be/pmyDJnEza6A

Published in: Technology, Business, Lifestyle
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,178
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Using Common Node to build StartHQ

  1. 1. Server-Side JS @ StartHQ Node, Fibers, Common Node etc.
  2. 2. 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
  3. 3. Fibers usage example function sleep(ms) { var fiber = Fiber.current; setTimeout(function() { fiber.run(); }, ms); Fiber.yield(); }
  4. 4. Sleep: Async. vs. Sync. console.log('before'); setTimeout(function() { console.log('after 1s'); }, 1000); console.log('before'); sleep(1000); console.log('after 1s');
  5. 5. 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
  6. 6. 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
  7. 7. Benchmarks
  8. 8. Node Streams var body = []; stream.on('data', function (chunk) { body.push(chunk); }) stream.on('end', function () { callback(body); });
  9. 9. Node Streams2 var body = []; stream.on('readable', function () { body.push(stream.read()); }) stream.on('end', function () { callback(body); });
  10. 10. Common Node Streams var body = [], chunk; while(chunk = stream.read(null)) { body.push(chunk); } return body; OR return stream.read();
  11. 11. Ecosystem ● Ringo modules ● Common Utils ● Mongo Sync ● Stick ● Wrapping Node packages
  12. 12. Common Node at StartHQ ● Use it for everything ○ API, static resources ○ background processing ○ command line tools ● Extractors API ● Sentry stack traces
  13. 13. 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
  14. 14. GitHub Links ● laverdet/node-fibers ● olegp/common-node ● olegp/notes ● starthq/extractor
  15. 15. Thank you! @alexlamsl

×