Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Time travel with javascript

Avatar for Alex Dong Alex Dong
August 05, 2013

Time travel with javascript

Aka those weird behaviors when you try to squeeze asynchronous behaviors into a co-operative multi-tasking single-threaded execution model.

Avatar for Alex Dong

Alex Dong

August 05, 2013

Other Decks in Programming

Transcript

  1. WTF? Teleporting.js? The code can swim upstream or downstream against

    time. The concept of sequence in javascript could be trickier than one expects. Tuesday, August 6, 13
  2. How javascript timer works? Timer delay in javascript is not

    guaranteed. All javascript in browser executes on a single threaded asynchronous events. When a new async event occurs, mouse click, XMLHttpRequest completion, will put it into a “waiting” queue. setTimeout(0, func) similar to ‘yield’ Tuesday, August 6, 13
  3. We’re not done yet Enters “process.nextTick(fn)” in node.js It runs

    “before any other I/O events fire”. Much more efficient if you’re doing node.js Tuesday, August 6, 13
  4. process.nextTick vs. setTimeout(0) Run Math.sqrt(i) for i in range(0, 1024*1024)

    Caution. This is what the doc says: It typically runs before any other I/O events fire, but there are some exceptions. Like what?! Tuesday, August 6, 13