getElementsByClassName Speed Comparison


Mark Finkle suggested that I do some speed testing, now that a native implementation of getElementsByClassName has landed in the Mozilla trunk (destined for Firefox 3).

So I went around and dug up all of the different, existing, implementations that I could find. Currently, implementations fall into one of three categories (with some straddling more than one):

  • Pure DOM
    This usually involves a calls to .getElementsByClassName("*") and traversing through all matched elements, analyzing each element’s className attribute along the way. Generally, the fastest method is to use a pre-compiled RegExp to test the value of the className attribute.
  • DOM Tree Walker
    Is a less-popular means of traversing DOM documents by setting some simple parameters, as specified by the DOM Level 2 Spec. For example, you could traverse through all text nodes in a document (something that you can’t easily do in any other way).
  • XPath
    The most recent technique, to be popularized, was the use of XPath to find elements by classname. The implementation is generally simple: Building a single expressions and letting the XPath engine traverse through the document, finding all the relevant elements.

I’ve chosen some implementations that were representative of each of these techniques.

Tree Walker

An implementation using the DOM Level 2 Tree Walker methods. Builds a generic filter function and traverses through all elements.

  1. document.getElementsByClass = function(needle) {
  2.   function acceptNode(node) {
  3.     if (node.hasAttribute("class")) {
  4.       var c = " " + node.className + " ";
  5.        if (c.indexOf(" " + needle + " ") != -1)
  6.          return NodeFilter.FILTER_ACCEPT;
  7.     }
  8.     return NodeFilter.FILTER_SKIP;
  9.   }
  10.   var treeWalker = document.createTreeWalker(document.documentElement,
  11.       NodeFilter.SHOW_ELEMENT, acceptNode, true);
  12.   var outArray = new Array();
  13.   if (treeWalker) {
  14.     var node = treeWalker.nextNode();
  15.     while (node) {
  16.       outArray.push(node);
  17.       node = treeWalker.nextNode();
  18.     }
  19.   }
  20.   return outArray;
  21. }

The Ultimate getElementsByClassName

Uses a pure DOM implementation, tries to make some optimizations for Internet Explorer.

  1. function getElementsByClassName(oElm, strTagName, strClassName){
  2.     var arrElements = (strTagName == "*" && oElm.all)? oElm.all :
  3.         oElm.getElementsByTagName(strTagName);
  4.     var arrReturnElements = new Array();
  5.     strClassName = strClassName.replace(/\-/g, "\\-");
  6.     var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
  7.     var oElement;
  8.     for(var i=0; i<arrElements.length; i++){
  9.         oElement = arrElements[i];      
  10.         if(oRegExp.test(oElement.className)){
  11.             arrReturnElements.push(oElement);
  12.         }  
  13.     }
  14.     return (arrReturnElements)
  15. }

Dustin Diaz’s getElementsByClass

A pure DOM implementation, caches the regexp, and is generally quite simple and easy to use.

  1. function getElementsByClass(searchClass,node,tag) {
  2.     var classElements = new Array();
  3.     if ( node == null )
  4.         node = document;
  5.     if ( tag == null )
  6.         tag = '*';
  7.     var els = node.getElementsByTagName(tag);
  8.     var elsLen = els.length;
  9.     var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  10.     for (i = 0, j = 0; i < elsLen; i++) {
  11.         if ( pattern.test(els[i].className) ) {
  12.             classElements[j] = els[i];
  13.             j++;
  14.         }
  15.     }
  16.     return classElements;
  17. }

Prototype 1.5.0 (XPath)

Mixes an XPath and DOM implementation; using XPath wherever possible.

  1. document.getElementsByClassName = function(className, parentElement) {
  2.   if (Prototype.BrowserFeatures.XPath) {
  3.     var q = ".//*[contains(concat(' ', @class, ' '), ' " + className + " ')]";
  4.     return document._getElementsByXPath(q, parentElement);
  5.   } else {
  6.     var children = ($(parentElement) || document.body).getElementsByTagName('*');
  7.     var elements = [], child;
  8.     for (var i = 0, length = children.length; i < length; i++) {
  9.       child = children[i];
  10.       if (Element.hasClassName(child, className))
  11.         elements.push(Element.extend(child));
  12.     }
  13.     return elements;
  14.   }
  15. };

Native, Firefox 3

A native implementation, written in C++; is a part of the current CVS version of Firefox, will be included in Firefox 3.

  1. document.getElementsByClassName

The Speed Results

For the speed tests I copied the Yahoo homepage into a single HTML file and used that as the test bed. They make good use of class names (both single and multiple) and is a considerably large file with lots of elements to consider.

You can find the test files, for each of the implementations, here:
http://ejohn.org/apps/classname/

Note: “XPath” is just Prototype’s implementation.

From these figures we can see that the native implementation of getElementsByClassName, in Firefox 3, is a full 8x faster than the XPath implementation. Additionally, it’s a stunning 77x faster than the fastest DOM implementation.

Note: These numbers have been revised from what was originally posted as the lazy-loading nature of document.getElementsByClassName wasn’t taken into account. The resulting arrays are completely looped-through now, making sure that all elements are accounted for.

Currently, Prototype has the best general-use implementation: Use XPath selectors wherever possible, fall back to fast DOM parsing.

Interestingly, only Prototype actually tries to implement the document.getElementsByClassName interface (all others do one-off names). However, Prototype doesn’t check to see if the document.getElementsByClassName property already exists, and completely overwrites the, incredibly fast, native implementation that Firefox 3 provides (oops!).

In all, the results are quite astounding. The native implementation is absolutely much faster than anything I could’ve imagined. It completely decimates all the other pieces of code. I can’t wait until this hits the general public – users will, absolutely, feel a significant increase in speed.

Posted: March 8th, 2007


If you particularly enjoy my work, I appreciate donations given with Gittip.

25 Comments (Show Comments)



Comments are closed.
Comments are automatically turned off two weeks after the original post. If you have a question concerning the content of this post, please feel free to contact me.


Secrets of the JavaScript Ninja

Secrets of the JS Ninja

Secret techniques of top JavaScript programmers. Published by Manning.

Ukiyo-e Database and Search

Ukiyo-e.org

Japanese woodblock print database and search engine.


John Resig Twitter Updates

@jeresig

Infrequent, short, updates and links.


via Ad Packs