As I submerse myself deeper and deeper into the world of JavaScript (although still not very deep), I continue to find intriguing ways of doing things. Lately, I’ve been wanting to test the performance of some various techniques, as well as native JavaScript versus jQuery.
There are numerous browser add-ons/plug-ins but that only really tells the story for that particular browser, and let’s face it, not all browser JavaScript engines were created equal. But in doing some Google searches, I found an interesting console method. Unfortunately, I can’t remember which site I saw it on, so I can’t give proper credit.
Anyways, the method looked like this:
console.time('foo');
// your JS code here
console.timeEnd('foo');
What it does, is it starts a timer, runs your JavaScript code, then stops the timer. The time it takes to run your code is then sent to the console for you to look it. Also, in case you are wondering what ‘foo’ represents…it’s just a reference you pass so the method knows what timer you are referring to.
With this, you can compare various functions and quickly see the total time it takes to run them, helping you determine which is more time-efficient.
It’s important to note, that this can’t be used in IE7- due to its lack of console, unless you use Firebug Lite. In IE8, you must open the Developer Tools and then you will be able to post to the console. All other browsers seem to work just fine.
