Archive for February, 2007

How to avoid parsing all the links on a page

February 5th, 2007 by Aaron N.

There are a lot of different reasons to parse all the links on a page. Lightbox (and it’s Mootools counterpart, Slimbox) look through all the links to find the ones that need their magic. SmoothScroll loops through them to find all the links with in-page name anchors. Here at CNET, we’re starting to use javascript to insert our tracking logic into our links (instead of hard-coding them in the links in our templates).

For a page like one on CNET, this can mean having the browser iterate through document.links several times, and the result can really bog down a page.

I ran this code on cnet’s home page:

$A(document.links).each(function(lnk, index){
    $(lnk).addEvent('click', function(){
        alert('hi');
    });
});

There are 332 links on the page, and this code took 190ms. Now, I could optimize this loop, but this is probably how someone who wasn’t working on optimize it might write it. For instance, using $$(‘a’) is actually twice is fast as $A(document.links).

Anyway, the problem is when you start adding these things up. If you solve this “I need to check all the links here” problem by iterating through, you might end up slowing the page down by half a second, which is when things start getting noticeable.

In a recent post on Ajaxian, I was directed to this article on using capture to steal the event. Using this method of adding events, you can add an event listener to the whole document.body. Then, on click, evaluate your code. If the link clicked meets your criteria, execute your code, else, follow the link.

Note that IE doesn’t use capturing, but I think you can still stop the event from propagating. I need to do some testing first.

Efficient Looping in Javascript

February 2nd, 2007 by Aaron N.

Bas Wenneker, on his blog solutoire.com just posted some data he compiled using various methods for iterating over a data set that’s pretty interesting.

While I’m a great fan of Javascript Libraries like Prototype and Mootools, I’m less happy with their iterators. Iterating through a large array just takes ages using Array.each(). I think the most annoying thing with Javascript is that it freezes the browser while it’s being processed. So I started Aptana and wrote a small benchmark, and I tested it with FF2, Opera 9.1 and IE6. I did the testing on WinXP SP2 running on my crappy AMD Duron 1600 (yes, it’s almost antique). In this article I present the results of the benchmark.

See the results »

Mootorial gets Array docs

February 2nd, 2007 by Aaron N.

That’s odd. Somehow I left out the section in the Mootorial about arrays. Weird. Anyway, it’s there now.

CNET launches beta version of new API

February 1st, 2007 by Bill Graham

Today CNET launched the beta version of its new API service. Referred to as the CNET API, this service will make a wealth of data available to the mashup community in the creation of rich consumer sites. The initial dataset will include CNET’s full catalog of technology products and software titles. This is the same dataset that drives the highly trafficked sites of CNET Reviews, Shopper.com and Download.com. Over time, the API will add news articles, community content, blog posts and full product reviews.
Read the rest of this entry »

Simple image gallery

February 1st, 2007 by Aaron N.

Continuing in my current project of rewriting all the javascript on Download.com (pity me), I encountered the functions on the screenshots page. A quick rewrite and here’s my SimpleGallery class. It’s not super-duper fancy but it’s not meant to be. Read the rest of this entry »