Quantcast
Channel:   eSolution Inc   » point
Viewing all articles
Browse latest Browse all 114

How to Trace JavaScript Performance Using Your Developer Console

$
0
0

There is one universal rule which applies to all websites and applications: they must be fast. (A few years ago I would have stated sites should be responsive, although that word now has RWD connotations).

Unfortunately, it’s evident that some developers are not concerned with site speed. If they did, multi-megabyte monstrosities would be rare and average page weights would never have exceeded 1Mb. It (almost) makes me long for the dial-up days when a 100Kb home page was considered excessive!

A lightweight and fast user experience has never been more important. Mobile and web use in developing countries is increasing exponentially and fast broadband has never been plentiful or inexpensive. Trimming page fat is vital and so is optimizing your JavaScript performance. Fortunately, there are a number of easy ways to determine whether your scripts are causing significant delays.

Console Timing

Firebug, the Webkit Inspector, Opera Dragonfly and IE11 all support the <code>console.time()code> and <code>console.timeEnd()code> methods, e.g.

// start timer
console.time("DOM update");
// update
var p = document.getElementById("result");
for (var i = 0; i < 3000; i++) {
	p.textContent += i + " ";
}
// end timer
console.timeEnd("DOM update");

A timer name is passed to <code>console.time()code> and <code>console.timeEnd()code>. You can have any number of timers running:

// start "DOM update" timer
console.time("DOM update");
// update
var p = document.getElementById("result");
// start "loop" timer
console.time("loop");
for (var i = 0; i < 3000; i++) {
	p.textContent += i + " ";
}
// end "loop" timer
console.timeEnd("loop");
// end "DOM update" timer
console.timeEnd("DOM update");

The results are shown in the developer console:

 How to Trace JavaScript Performance Using Your Developer Console

Profiling Timestamps

Another option is time-stamping. This is only available in Firebug and the Webkit Inspector — it records a point during execution, e.g.

// record timestamp
console.timeStamp("DOM update started");
// update
var p = document.getElementById("result");
for (var i = 0; i < 3000; i++) {
	p.textContent += i + " ";
}
// record timestamp
console.timeStamp("DOM update ended");

Firebug displays when a time-stamp was encountered:

 How to Trace JavaScript Performance Using Your Developer Console

That may help a little, but the Webkit Inspector has a few more tricks. Click the Timeline tab followed by the Record icon in the lower-left of the window:

 How to Trace JavaScript Performance Using Your Developer Console

Refresh your page then hit the Record icon again to stop recording. The time-line shows a visual representation of all events with your timeStamps marked in yellow in the timer bar:

 How to Trace JavaScript Performance Using Your Developer Console

Very useful.

Removing Your Timers

Ideally, you should never leave console logging in production code. If you do, you’ll need to test whether the methods are available since they can cause errors in browsers where they’re not supported, e.g.

if (console && console.timeStamp) console.timeStamp("My timestamp");

Personally, I recommend removing all console commands. Some build processes will do this for you, or you could use the following regular expression in your editor to target and remove all timer-related logging:

<code>console\.time[^\(]*\("[^"]+"\);code>

Open your console and start optimizing those scripts!

 How to Trace JavaScript Performance Using Your Developer Console

Try Learnable for $9

For only $9, you can try Learnable for 30 days AND download two SitePoint ebooks of your choice!

Extended due to popular demand. Ends Wed 17 July.

Try now

 How to Trace JavaScript Performance Using Your Developer Console

Viewing all articles
Browse latest Browse all 114

Trending Articles