set attributes

JavaScript performance comparison

Revision 3 of this test case created by tomByrer

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>

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
if (!document.getElementsByClassName) {
  document.getElementsByClassName = function(search) {
    var d = document, elements, pattern, i, results = [];
      elements = d.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;
  }
}
var elements = document.getElementsByClassName('vanilla');
for (var i = 0; i < elements.length; i++) {
    elements[i].setAttribute('title','vanilla');
}
pending…
combo
var elements = $('.combo');
for(var i = 0, length = elements.length; 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