August 13, 2009 Leave a comment
Most web pages nowadays contain big chunks of java scripts that help pages to be more appealing. However, java scripts affects web pages performance because browsers must execute code within all the script tags before rendering the page. That’s why the more script you have on your page, the more delay your page is gonna suffer before starting to render. That’s why one must make sure to use efficient scripts that does not affect the page performance. But how do you measure your script performance. Well, there are two ways to measure your java script function performance, whether manually or using a performance profiling tool.
1- Manually testing your java script function performance involve one writing custom code to test how long his function takes to execute. This technique is really simple because all what you need to do is to add a line of code at the beginning of the function and another one at the end to calculate the time the function took to finish.
var started = new Date().getMilliseconds();
//Your code Here
var ended = new Date().getMilliseconds();
Alret ("Function MyFunction executed in" + stop - start);
As you can see, what I have done is adding a statement to get the time at the beginning of the function and another one to get the time at the end of the function then subtract them to calculate how long did this function take.
2- The other way to measure the performance of your java script function is to use a performance profiler tool which would give you a complete picture of your page execution. The most famous tool is yslow which is an addons on firefox.