Skip to content

JavaScript measuring execution time

JavaScript performance is becoming increasingly important, it is good to know basic instrumentation techniques. One of the basic instrumentation tool is console.time and console.timeEnd.

console.time() starts a new timer with an associated label. When console.timeEnd() is called with the same label, the timer is stopped and the elapsed time is displayed in the console. Timer values are accurate to the sub-millisecond. The strings passed to These functions must match or else the timer will not finish.

Following is the simple example, which will show you how to measuring execution time.

console.time('loop');
var i = 10;
while ( i-- ) console.log( i );
console.timeEnd('loop');

Output:

JavaScript measuring execution time

Note: The only Firefox returns the elapsed time in the timeEnd() call. The other browsers simply report the result to the developer console: the return value of timeEnd() is undefined.

These functions, supported by modern browsers, in starting with Firefox11+, Chrome2+,Safari 4+ and IE11+

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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments