Caching Nodelists

April 30, 2010

Bug 33696 changed how some caches behave in WebKit, for a ~20% speed improvement in the Dromaeo benchmark.

For context, Dromaeo is a browser benchmark created by John Resig of jQuery fame. As compared to the V8 and SunSpider benchmarks (which focus on JavaScript performance), Dromaeo touches more of the code where JavaScript interacts with the browser.

But there was a catch: this caching change violated the letter of the HTML5 spec. The spec described a sensible but cache-unfriendly behavior, in that multiple calls to getElementsByTagName would each return a new object. That means that this code:

var x = document.getElementsByTagName('a'); = 1;
var y = document.getElementsByTagName('a');

would have different behavior before and after this change.

Some head-scratching and digging into Mozilla code (browser development: so much easier when the browser vendors can just say, "let's see what {Mozilla,WebKit} does") found that Mozilla had already implemented such a cache, so it seems unlikely any web developers relied on either implementation of this behavior. And additionally the spec was inconsistent in its wording for related functions.

So the conclusion was to change the spec. I imagine this is exactly the sort of reason HTML5's intended timeline is so far out in the future. (Speaking of which, I recently saw someone dig up this old interview with Hixie which I found pretty interesting.)

The whole bug is worth a read.