Archive for March, 2006

Favorite Loop Benchmark

Thursday, March 30th, 2006

Thanks to a suggestion from Johnathan Snook, here is a summary of the results of the benchmarking I did on my favorite loop:

Method 1 uses for (var i=0; i<test.length; i++)
Method 2 uses for (var i=0, l=test.length; i<l; i++)
Method 3 uses for (var i=0, o; o=test[i]; i++)

1000 Iterations
Method Firefox 1.5 PC IE 6.0 PC Safari
1 0ms 16ms 11ms
2 0ms 0ms 12ms
3 0ms 0ms 10ms
10000 Iterations
Method Firefox 1.5 PC IE 6.0 PC Safari
1 31ms 32ms 125ms
2 16ms 15ms 117ms
3 16ms 16ms 96ms
100000 Iterations
Method Firefox 1.5 PC IE 6.0 PC Safari
1 297ms 266ms 1937ms
2 296ms 250ms 1924ms
3 250ms 203ms 1878ms

Here’s a link to the script I used for benchmarking.

My New Favorite Loop

Wednesday, March 29th, 2006

I recently discovered a new way to loop through an array in JavaScript that doesn’t use the standby Array.length property to determine the number of iterations:


var someArray = ["Dogs", "Cats", "Chickens"];
for (var a=0, o; o=someArray[a]; a++) {
   document.write(o + ‘<br/>’);
}

Here is how it works. The initial expression part of the loop var a=0, o sets the initial value of a to zero, which is the first element in our array and at the same time initializes a variable named o. The o variable is important as it will eventually hold the individual elements from our array as we iterate through it.

The next part of the loop - the conditional - is probably different from the traditional condition used when iterating through a loop. Most texts have you test the the counter or iterator value against the total number of elements in the array. This normally look like a<someArray.length

In my example, we are assigning an element of the array into the o variable that was initialized in the initial expression. The element that is assigned, is based on the current value of the counter variable a. As long as there is an element in the array at the counter value, JavaScript evauates the for loop’s conditional statement as true, and the loop’s statement block executes. The best part here and in my opinion the clever aspect of this method, is that the variable o now contains the value of the array and can be used in the loop statement block without having to re-resolve the array element value.

The loop also performs quite well as JavaScript does not have to evaluate someArray.length each iteration through the loop. In fact I performed a few benchmark tests on the various versions of this loop and for iterations under 1000 the methods perform equally well. When I bump up the iterations to 10000 or more, the method I’ve described in this article, performs up to twice as fast!

I’ve also seen versions of this loop -

for (var a=0, len=someArray.length; a<len ; a++)

- where the array length is only evaluated once. This method performs slower in my benchmarks than the method described in this article.

One note here, this method is not appropriate for looping through arrays that contain either numbers or boolean values, as assigning the number 0 into a variable resolves as false in the conditional part of the loop.

Rails 1.1 Released

Tuesday, March 28th, 2006

Rails 1.1 was released earlier today.  This is the largest upgrade to the popular web app framework with RJS, Active Record, and 500 other things being improved!

JavaScript Debugging in Internet Explorer

Sunday, March 26th, 2006

A nice tip at the IEBlog reminded me of something I knew a long time ago, but forgot since I began doing most of my JavaScript development testing with Firefox.  My favorite features in Firefox are those rich extensions that make debugging JS a pleasure and the fact that Firefox shows you exactly (file and line number) where your JS errors are.  Turns out IE has had this for a while and the post Scripting Debugging in Internet Explorer show you how.  Simply uncheck a couple of IE options and install the Microsoft Script Debugger and your getting much closer to Firefox-style JS debugging!

How to Open Popup Windows

Sunday, March 26th, 2006

Jehiah Czebotar has written a nice article that illustrates a best practice for opening popups windows.  He offers a solution that is degradable and semantically correct.

JavaScript Eclipse Plugin

Sunday, March 26th, 2006

JSEclipse is a JavaScript Eclipse plugin that has some very cool features.  I like the ability to create your own code completion definitions for the functions that you create or other libraries like Prototype or Dojo.  Although not a free product, for only $29 it’s a nice addition to your Eclipse plugin library.   You can also try the unrestricted free for non-commercial use version.

Anti-Aliased Rounded Corners with CSS

Sunday, March 26th, 2006

Spiffy Corners is a CSS technique for making anti-aliased rounded corners on your HTML boxes.  The result is very nice considering the solution uses no images or JavaScript.

Using Subversion for Web Development

Sunday, March 26th, 2006

SitePoint has a nice introduction on Using SVN for Web Development.  Subversion (SVN) is a version control system that help in the management of software projects being developed by large or small groups.  This article may help you decide if SVN is right for your team.

Open-Source Date Slider

Saturday, March 25th, 2006

This one is a bit old, but in the event that you missed it, you should really check out Measure Map’s open-source Date Slider. The slider is a Flash component that allows you to select a range of dates and present nice summary data based on the date range. Measure Map is a slick web-based analyitics application that is easy to install on virtually any blog.

Degradable Ajax

Saturday, March 25th, 2006

The Particletree blog has an excellent tutorial on The Hows and Whys of Degradable Ajax. The technique involves creating a page that works like a traditional web application that processes information on page loads and refreshes. Then, if JavaScript is enabled, they have their scripts bypass the traditional functionality and replaces it with Ajax functionality.