SlideShare ist ein Scribd-Unternehmen logo
1 von 15
Downloaden Sie, um offline zu lesen
Server-Side JS @ StartHQ
Node, 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

Weitere ähnliche Inhalte

Mehr von Oleg Podsechin

Current State of Server Side JavaScript
Current State of Server Side JavaScriptCurrent State of Server Side JavaScript
Current State of Server Side JavaScript
Oleg Podsechin
 
The future of server side JavaScript
The future of server side JavaScriptThe future of server side JavaScript
The future of server side JavaScript
Oleg Podsechin
 

Mehr von Oleg Podsechin (11)

Lean and mean MongoDB
Lean and mean MongoDBLean and mean MongoDB
Lean and mean MongoDB
 
RingoJS
RingoJSRingoJS
RingoJS
 
JS everywhere 2011
JS everywhere 2011JS everywhere 2011
JS everywhere 2011
 
What every developer can learn from startups
What every developer can learn from startupsWhat every developer can learn from startups
What every developer can learn from startups
 
Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the way
 
Current State of Server Side JavaScript
Current State of Server Side JavaScriptCurrent State of Server Side JavaScript
Current State of Server Side JavaScript
 
On Platforms
On PlatformsOn Platforms
On Platforms
 
Common Node
Common NodeCommon Node
Common Node
 
The future of server side JavaScript
The future of server side JavaScriptThe future of server side JavaScript
The future of server side JavaScript
 
RingoJS
RingoJSRingoJS
RingoJS
 
Grid and Cloud Computing Intro
Grid and Cloud Computing IntroGrid and Cloud Computing Intro
Grid and Cloud Computing Intro
 

Kürzlich hochgeladen

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 

Using Common Node to build StartHQ

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