Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>setTimeout does not have a minimal delay of <code>0ms</code>. The minimal delay is anywhere in the range of 5ms-20ms dependent on browsers.</p> <p>My own personal testing shows that <code>setTimeout</code> doesn't place your back on the event stack immediately</p> <p><a href="http://jsfiddle.net/km5tY/63/">Live Example</a></p> <p>It has an arbitary minimal time delay before it gets called again</p> <pre><code>var s = new Date(), count = 10000, cb = after(count, function() { console.log(new Date() - s); }); doo(count, function() { test(10, undefined, cb); }); </code></pre> <ul> <li>Running 10000 of these in parallel counting to 10 takes 500ms. </li> <li>Running 100 counting to 10 takes 60ms.</li> <li>Running 1 counting to 10 takes 40ms. </li> <li>Running 1 counting to 100 takes 400ms.</li> </ul> <p>Cleary it seems that each individual <code>setTimeout</code> has to wait at least 4ms to be called again. But that's the bottle neck. The individual delay on <code>setTimeout</code>.</p> <p>If you schedule a 100 or more of these in parallel then it will just work. </p> <p>How do we optimise this?</p> <pre><code>var s = new Date(), count = 100, cb = after(count, function() { console.log(new Date() - s); }), array = []; doo(count, function() { test(10, array, cb); }); </code></pre> <p>Set up 100 running in parallel on the same array. This will avoid the main bottleneck which is the setTimeout delay.</p> <p>The above completes in 2ms.</p> <pre><code>var s = new Date(), count = 1000, cb = after(count, function() { console.log(new Date() - s); }), array = []; doo(count, function() { test(1000, array, cb); }); </code></pre> <p>Completes in 7 milliseconds</p> <pre><code>var s = new Date(), count = 1000, cb = after(1, function() { console.log(new Date() - s); }), array = []; doo(count, function() { test(1000000, array, cb); }); </code></pre> <p>Running a 1000 jobs in parallel is roughly optimum. But you will start hitting bottlenecks. Counting to 1 million still takes 4500ms.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload