Node.js Development in 2020: trends and techniques

Node.js Development in 2020: trends and techniques

A2443b68836f2d166eae52b940e99bf6?s=128

Nikita Galkin

November 08, 2019
Tweet

Transcript

  1. Node.js Development in 2020: trends and techniques by Nikita Galkin

    Nov 8, 2019
  2. What anniversary is today?

  3. None
  4. None
  5. Why Node.js won the market? 1. Everybody knows JavaScript 2.

    Everybody loves Open Source 3. Google pay for V8 4. Performance boost every release 5. There is a release schedule
  6. None
  7. nvm install 12.13.0 nvm alias default 12.13.0 nvm reinstall-packages 10.16.3

    Migrate your local version easy with nvm:
  8. Node.js v12 Breaking changes

  9. None
  10. None
  11. Breaking change at Event Loop for Timers and Microtasks

  12. setTimeout(() => console.log('timeout1')); setTimeout(() => { console.log('timeout2'); Promise.resolve().then(() => console.log('promise

    resolve') ); }); setTimeout(() => console.log('timeout3')); setTimeout(() => console.log('timeout4'));
  13. None
  14. 6.8 ➜ 7.7

  15. ➜ ~ nvm use v10 Now using node v10.16.3 (npm

    v6.9.0) ➜ ~ node --print process.versions.v8 6.8.275.32-node.54 ➜ ~ nvm use v12 Now using node v12.13.0 (npm v6.12.0) ➜ ~ node --print process.versions.v8 7.7.299.13-node.12 6.8 ➜ 7.7
  16. const array = []; for (let i = 1; i

    < 10000; i++) array.push(i); console.time('assign'); const assign = array.reduce( (acc, curr) => Object.assign(acc, {[curr]: curr}), {}); console.timeEnd('assign'); console.time('spread'); const spread = array.reduce( (acc, curr) => ({...acc, [curr]: curr}), {}); console.timeEnd('spread');
  17. None
  18. 1. Faster JavaScript parsing 2. Faster async functions and promises

    3. Zero-cost async stack traces
  19. const { promisify } = require('util'); const wait = promisify(setTimeout);

    async function testAsyncStacktrace() { await wait(10); await willDie(); return 42; } async function willDie() { await Promise.resolve(); throw new Error('#Feelsbadman'); } testAsyncStacktrace() .catch(error => console.log(error.stack));
  20. None
  21. By default stack-trace is 10 On CLI level ➜ ~

    node async-stack-trace.js \ --stack-trace-limit=1000 On code level Error.stackTraceLimit = Infinity; More
  22. Node.js features

  23. Node.js v12 disappointments 1. ECMAScript Modules support is not stable.

    Again. 2. Most native modules are callback based. Only fs and dns modules have promises.
  24. JavaScript designed for browser where a failure affects only one

    tab, only one user. But at Node.js failing affect everything...
  25. Worker Threads are no longer experimental!

  26. None
  27. New http parser llhttp based on llparse

  28. 1. You use it! 2. For your own parser 3.

    TypeScript Why learn how it works?
  29. native Promises

  30. 1. Faster JavaScript parsing 2. Faster async functions and promises

    3. Zero-cost async stack traces
  31. What need to know: 1. promisify from util 2. once

    from events 3. Reading streams asynchronously
  32. function wait(timeout) { return new Promise(resolve => { setTimeout(resolve, timeout));

    }); } // VS const util = require('util'); const wait = util.promisify(setTimeout);
  33. const { EventEmitter } = require('events'); const emitter = new

    EventEmitter(); async function getPromise() { return new Promise(resolve => { emitter.once('eventName', resolve); }) }
  34. const { EventEmitter } = require('events'); const { once }

    = require('events'); const emitter = new EventEmitter(); async function getPromise() { return once( emitter, 'eventName'); }
  35. const { EventEmitter } = require('events'); const emitter = new

    EventEmitter(); async function getPromise() { return new Promise(resolve => { emitter.once('eventName', resolve); }) }
  36. async function main(filePath) { const readStream = fs.createReadStream(filePath); for await

    (const chunk of readStream) { console.log('>>> '+chunk); } console.log('### DONE ###'); }
  37. What need to use: 1. Promise.all/Promise.race 2. p-limit 3. multipleResolves

    from process
  38. Next big thing is QUIC/HTTP3

  39. None
  40. Node.js news

  41. 1. Node.js Foundation and JS Foundation Merge to Form OpenJS

    Foundation 2. Node.js certification 3. Nodejs.dev arrived
  42. None
  43. 1. Boolean 2. Null 3. Undefined 4. Number 5. String

    6. Symbol 7. Object Data types in JavaScript.
  44. 1. Boolean 2. Null 3. Undefined 4. Number 5. String

    6. Symbol 7. Object 8. BigInt Data types in JavaScript.
  45. Not only Node.js news

  46. 1. TC39 new proposals 2. GraphQL is not hype and

    has foundation. 3. Nestjs is the most promising framework
  47. TypeScript 3.7

  48. None
  49. HAPPY NODE.JS DEVELOPMENT! You can find me on Twitter as

    @galk_in Slides are available at speakerdeck.com/galkin or at my site galk.in