title: What is the reason JavaScript setTimeout is so inaccurate?
body: <p>I got this code over here:</p>
  
  <pre><code>var date = new Date();
  setTimeout(function(e) {
      var currentDate = new Date();
      if(currentDate - date &gt;= 1000) {
           console.log(currentDate, date);
           console.log(currentDate-date);
      }
      else {
         console.log("It was less than a second!");
         console.log(currentDate-date);
      }
  }, 1000);
  </code></pre>
  
  <p>In my computer, it always executes correctly, with 1000 in the console output. Interestedly in other computer, the same code, the timeout callback starts in less than a second and the difference of <code>currentDate - date</code> is between 980 and 998.</p>
  
  <p>I know the existence of libraries that solve this inaccuracy (for example, <a href="https://github.com/mrchimp/Tock" rel="noreferrer">Tock</a>).</p>
  
  <p>Basically, my question is: <strong>What are the reasons because <code>setTimeout</code> does not fire in the given delay?</strong> Could it be the computer that is too slow and the browser automatically tries to adapt to the slowness and fires the event before?</p>
  
  <p>PS: Here is a screenshot of the code and the results executed in the Chrome JavaScript console:</p>
  
  <p><img src="https://i.stack.imgur.com/53GNS.png" alt="Enter image description here"></p>