Wednesday, December 26, 2007

WebKit gets native getElementsByClassName

 

getElementsByClassName has always been a pain in the arse for us developers. Why it wasn't implemented natively across the board is something that browser folk can chat about. Not having it available has caused hacks, workarounds, and bugs.

Firefoxand Opera support the beast, and now Webkit has joined them:

The advantages of a native implementation are clear:

  • No additional JavaScript library files required
  • Clearly specified and consistent behavior
  • Blindingly fast

Howfast? Let’s have a look at WebKit’s shiny new implementation. For testing purposes I wrote a simple benchmark allowing comparison between three different methods for getting elements by their class names. The first is the new native getElementsByClassName, and the last two are both from prototype.js; one uses XPath, and the other is a straight JavaScript/DOM implementation.