January 14th, 2008 Posted by Don Albrecht
A question emerged out of my boredom on my flight yesterday. Which is faster, implicit vs explicit conversion. Most javascript developers use implicit conversion out of habit. For example:
!!x; instead of Boolean( x ); and x + “”; instead of String( x);.
I decided to try an experiment for myself and record the performance of casting a number to a Boolean or String on my Windows box in Safari 3, Firefox 2, Opera 9 and IE 7.
The Verdict:
Implicit conversion wins handily, demonstrating over a 7 fold performance increase in one test. Overall, the performance gain for using implicit conversion averaged out to 53% across browsers after 10 tests.
The Numbers:
| |
Implicit Boolean |
Explicit Boolean |
Implicit String |
Explicit String |
| Firefox 2 |
0.162 |
0.312 |
0.248 |
0.358 |
| IE 7 |
0.042 |
0.100 |
0.074 |
0.152 |
| Opera 9 |
0.030 |
0.088 |
0.020 |
0.142 |
| Safari 3 |
0.028 |
0.036 |
0.074 |
0.100 |
| Cross Browser Average |
0.066 |
0.134 |
0.104 |
0.188 |
You can find the code I used after the jump.
Keep reading →
November 27th, 2007 Posted by Don Albrecht
What is it:
FastInit is a small JS file to create a Faster window.onload. It implements a powerful onload function queue that fires once teh dom is ready and before all images have been downloaded. Ideally, this results in a dramatic decrease in a pages initialization time.
How it Works
You any functions to the queue by using the FastInit.addOnLoad( function 1, function 2, …); function. All functions are fired in the order in which they were added to the queue. addOnLoad can be called as many times as is necessary. It fails gracefully to standard handlers if the browser doesn’t support the faster methods.
Requirements
As of version 1.4.1 the need for Prototype has been removed freeing the code for use in any project where a speedy onload method is needed.
Check it out here:
http://tetlaw.id.au/view/javascript/fastinit
November 2nd, 2007 Posted by Don Albrecht
D’bug has published a wonderful list of techniques for abbreviating Javascript and improving performance.
You can find the article here:
http://blog.reindel.com/2007/11/01/javascript-shorthand-tips-and-tricks/