Javascript: Check execution time of code : Improve app performance

Basically, almost everybody can code. However, code is not the only ingredient to build great apps. Performance matters a lot. Now a days developers are more concerned about performance which mostly depends on execution speed of code. On the other hand coding standards and readability of codes are important topics too.

Therefore, to overcome the dilemma I have written an example with which one can choose  between two snippets of working codes.

console.time('timer_label')
var $elm1 = $('#highlighter_510863 table td:first-child div:first-child code:first-child');
$.each($elm1, function () {
  console.log($elm1.length)
});
console.timeEnd('time_label')
//VM10671:10 time_label: 10.416ms

Or, you could make a function and passing a block of code inside it which you want to measure

function measureTime(functionToExecute){
   console.time('exec_time');
   functionToExecute();
   console.timeEnd('exec_time');
}
measureTime(function(){
   var $elm1 = $('#highlighter_510863 table td:first-child div:first-child code:first-child')
   $.each($elm1,function(){
   console.log($elm1.length)
 });
});

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s