Javascript

Async & Performance

Contents

  • Async behaviour
  • Callback
  • Promise
  • Generator
  • Async / await
  • Performance

Asynchronous Running

  • Event Loop / Queue
  • Provides non-blocking execution

Callbacks

  • A function for future call
  • Bad parts
    • Callback hell
    • Early call
    • Not called at all
    • Called few or many times
    • Fail to pass parameters
    • Swallow error/exceptions

Promises

  • ES6 Native type
  • Solves trust issues of callbacks
  • Many good reasons to use it

Generators

  • yield keyword
  • python has also generators
  • Pausing execution of function
  • Increase iteration capabilities

async / await

  • Promise  + generator
  • Better and readable code
  • Looks like sync but async

Performance

  • https://jsperf.com/
  • Donald Knuth:
    • "Premature optimization is the root of all evil."
  • If you really don't sure about your need to optimize, simply don't do it

Web Workers !?

Look if you curious