Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Performance Evaluation [closed]

Tags:

javascript

background:

I've searched around for a reference, or possibly a tool, that helps you theoretically evaluate the efficiency (resource cost) of your JavaScript. This search has turned up a lot of excellent debugging software, but I can't really find something that helps me optimize the code, by utilizing less resource-intensive methods.

question:

Is there any resource (online guide, list, database, book, anything) or perhaps some software (web-based, browser plugin, IDE extension) that will help you optimize your JavaScript?

example:

innerText in IE / textContent in Firefox requires far fewer resources than innerHTML in either browser.

That one is kinda common sense, because it's less powerful, but there are other comparisons I hear about on a daily basis, and can't really verify if they are in fact better for optimized code or more efficient, and even if I could I have no way to test it!

Any ideas?

like image 753
NateDSaint Avatar asked Jul 20 '09 19:07

NateDSaint


4 Answers

In the same line as strife25, firebug has a very handy method of measuring time without handling any dates. Just use this:

console.time("Your timer name");
//Execute some javascript
console.timeEnd("Your timer name");

Then, check the console. alt text http://aquate.us/u/62232567893647972047.jpg

Edit -- off by 30 odd seconds. :(

like image 132
Salty Avatar answered Oct 14 '22 10:10

Salty


the usual way to evaluate Javascript is by evaluating the amount of time it takes for a set of code to execute:

var begin = new Date().getTime();
//do stuff
console.debug( new Date().getTime() - begin );

However, there are some issues with this in IE. if a script takes <15ms to run, IE returns 0ms as the result.

Various javascript libraries have testing frameworks to help evaluate your code's speed. Dojo's testing framework is called DOH.

John Resig also made a firebug plugin called FireUnit which allows you to easily evaluate the time it takes for a function to execute, and with a little configuring, also outputs the Big O of a function, which is a great piece of data.

Check out Resig's video from JSConf on JS performance testing:

Measuring Javascript Performance - John Resig

FireUnit rundown

like image 24
linusthe3rd Avatar answered Oct 14 '22 11:10

linusthe3rd


I always liked the simple approach with Firebug:

console.time("timer-name");

and

console.timeEnd("timer-name");

Good for granular level measurements.

like image 3
Mike Robinson Avatar answered Oct 14 '22 09:10

Mike Robinson


Firebug's 'profile' tool is great for measuring javascript performance. It shows you all the bottlenecks in your code in a function by function breakdown, showing which one's had the highest average time, most calls, etc.

like image 1
TJ L Avatar answered Oct 14 '22 10:10

TJ L