javaScript benchmarking : Console : Compare execution time between two functions / statements

If you want to be one of the best programmers and you are best practice enthusiast then you might wonder for minutes thinking about the standard of the code you have just written or about to write. If you are working on sensitive application that must pass certain performance bench mark then you have to compare the execution time of the statements or functions you are up to. Continue reading

Advertisements

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)
 });
});