JavaScript: Tracing performance to the console

I wanted to trace the performance of Ajax call in a project I’m working on. The obvious place to store this kind of information is the developer console. If your browser supports debugging, you can writen to the console object and display it in the browser. You should be able to open it in most browsers by pressing F12 or Ctrl-Shift-I.

Elapsed time

You’ve probably already used console.log or console.debug but the console global JavaScript object does provide many more functions. Two of them which are useful in our use case are:

  1. console.time()
  2. console.timeEnd()

They both take a timer name and when time() is called a timer with this name will be started. When timeEnd() is called the specified timer is stopped and the elapsed time since its start in milliseconds is logged to the console at the Info level. Here is how the ouput looks like in Firefox:

console time

Your code would look like this:

// refresh the grid
// fill the grid

You can also have an outer timer logging the total time an operation lasts and some inner timers logging the time for individual steps:


// refresh the grid

// fill the grid



Another function of the console global JavaScript object which you could use is console.timeStamp() e.g.:

// fill the grid

This will output the following to the console:

console timeStamp

If you are using Firefox, please note that the Firefox Web Console doesn’t support the console.timeStamp(). But Firebug does. So if you want to use it, you’ll need to install Firebug which is an awesome plugin anyway.

Check for existence

Another thing you have to keep in mind is that although these console functions are pretty well supported by most popular browsers they do not seem to be part of any specification and you might find some browsers (e.g. mobile browser) which do not support them. Calling these functions in such a browser will cause you JavaScript code to fail. So you should either make sure that you only use this in your development environment but do not push it to you production site or at least check whether the console object and its functions exist:

if (console && console.time) { console.time("test"); }
if (console && console.timeEnd) { console.timeEnd("test"); }
if (console && console.timeStamp) { console.timeStamp("test"); }

Internet Explorer 8 doesn’t support the console global JavaScript object. Internet Explorer 11 does. It also looks like Internet Explorer 10 also supports this but I couldn’t check it.

Network calls

If all you’re after is to check how long your Ajax calls to the server last, you can just use the network call analyzer available in the developer tools of your favorite browser.

In Chrome it looks like this:

Network tab Chrome

In Firebug (Firefox plugin):

Network tab Firebug

Note that in Firefox you can get the same information without Firebug, in the Web Console:

Network tab Firefox

It’s still a good idea to use Firebug which is a great plugin. But you don’t need it if all you’re after is this type of information.


Leave a Reply

Your email address will not be published. Required fields are marked *