• About
  • Twitter
  • Github
  • About
  • Twitter
  • Github
  • Sorry, I simply don't trust in Console.Time or neither similar

    07 Apr • 2015

    It’s normal for measure time. Many developers choose the console.time or in some cases use Date.getTime().

    I give you a simple example:

    The Code

    console.time('execution time: ');
    var total = 0;
    for (var i = 0; i <= 1e2; i++) {     total += i; }
    console.log(total);
    console.timeEnd('execution time: ');

    Results

    Chrome (version 41.0)

    Firefox (version 37.0.1)

    But, don’t trust 100% in any of these.

    Why I’m saying that?

    If you need to measure time precisely, neither console.time() or using Date.getTime() will ,get you far. Using Date.getTime() you got something like this:

    var start = (new Date).getTime(); /* Run a test. */ var diff = (new Date).getTime() - start;

    Even if you repeat the same code, same syntax and the same logic. It can get quite different results when run.

    You can see what I say using the Dromaeo (a Mozilla JavaScript performance test suite) or you browser console. So you realize the time in the implementation of JavaScript isn’t something that you can be measuring with full certainty.

    The truth is: JavaScript execution time can be quite affected by so many things, like the engine used by the browser or the user machine.

    You can check out John Resig’s blog post about the accuracy of JavaScript time to learn more about that.