Set attributes

JavaScript performance comparison

Revision 5 of this test case created by Chen-Pang He

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<p class="jquery">test</p>
<p class="jquery">test</p>
<p class="vanilla">test</p>
<p class="vanilla">test</p>
<p class="combo">test</p>
<p class="combo">test</p>
<script>
if (!document.getElementsByClassName) {
  document.getElementsByClassName = function(search) {
    var elements, pattern, i, results = [];
    if (document.querySelectorAll) { // IE8
      return document.querySelectorAll("." + search);
    }
    if (d.evaluate) { // IE6, IE7
      pattern = ".//*[contains(concat(' ', @class, ' '), ' " + search + " ')]";
      elements = document.evaluate(pattern, document, null, 0, null);
      while ((i = elements.iterateNext())) {
        results.push(i);
      }
    } else {
      elements = document.getElementsByTagName("*");
      pattern = new RegExp("(^|\\s)" + search + "(\\s|$)");
      for (i = 0; i < elements.length; i++) {
        if ( pattern.test(elements[i].className) )
          results.push(elements[i]);
      }
    }
    return results;
  }
}
</script>

Preparation code output

test

test

test

test

test

test

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
jquery
$('.jquery').attr('title', 'jquery')
pending…
vanilla
var elements = document.getElementsByClassName('vanilla')
var length = elements.length
for (var i = 0; i < length; i++) {
  elements[i].setAttribute('title','vanilla')
}
pending…
combo
var elements = $('.combo')
var length = elements.length
for (var i = 0; i < length; i++) {
  elements[i].setAttribute('title','combo')
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment