console.time & console.timeEnd

By  on  

The console.time and console.timeEnd methods allow developers to time any routine and get a duration in milliseconds.  Since JavaScript performance is becoming increasingly important, it's good to know basic techniques for benchmarking routines.  One of the most basic benchmarking tools is console.time with console.timeEnd.

console.time starts the time and console.timeEnd stops the timer and spits out the duration:

// Kick off the timer
console.time('testForEach');

// (Do some testing of a forEach, for example)

// End the timer, get the elapsed time
console.timeEnd('testForEach');

// 4522.303ms (or whatever time elapsed)

Passing a timer name as the first argument allows you to manage concurrent timers.  The console.timeEnd call immediately spits out the elapsed time in milliseconds.

There are more advanced techniques for performance testing and benchmarking but console.time/timeEnd provide a quick manual method for speed testing!

Recent Features

  • By
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

  • By
    How to Create a Twitter Card

    One of my favorite social APIs was the Open Graph API adopted by Facebook.  Adding just a few META tags to each page allowed links to my article to be styled and presented the way I wanted them to, giving me a bit of control...

Incredible Demos

  • By
    CSS Columns

    One major gripe that we've always had about CSS is that creating layouts seems to be more difficult than it should be. We have, of course, adapted and mastered the techniques for creating layouts, but there's no shaking the feeling that there should be a...

  • By
    Face Detection with jQuery

    I've always been intrigued by recognition software because I cannot imagine the logic that goes into all of the algorithms. Whether it's voice, face, or other types of detection, people look and sound so different, pictures are shot differently, and from different angles, I...

Discussion

  1. no necessary label, the default label is default

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!