classList v old way

JavaScript performance comparison

Revision 4 of this test case created by m_gol

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div data-fixture id="a">
</div>
<div data-fixture id="b">
</div>
<script>
  var a = document.getElementById("a"),
      b = $("#b");

  function addClasses(el, classes) {
    classes = classes.split(" ")
    for (var i = 0, l = classes.length; i < l; i++) {
      el.classList.add(classes[i]);
    }
  }
</script>
<script>
Benchmark.prototype.setup = function() {
    var classesNum = 50
    classes = "foo0";
    for (var i = 1; i < classesNum; i++) {
      classes += " foo" + i;
    }
};

Benchmark.prototype.teardown = function() {
    $("[data-fixture]").attr("class", "");
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
classList
for (var i = 0; i < classesNum; i++) {
  a.classList.add("foo" + i);
}
pending…
classList wrapped
addClasses(a, classes);
pending…
jQuery
b.addClass(classes);
pending…
jQuery separate
for (var i = 0; i < classesNum; i++) {
  b.addClass("foo" + i);
}
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