classList v old way

JavaScript performance comparison

Revision 12 of this test case created by James Hartig

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>
<div data-fixture id="c"></div>
<div data-fixture id="d"></div>

<script>

var a = document.getElementById("a"),
    c = document.getElementById("c"),
    b = $("#b"),
    d = $("#d"),
    i, arr;

</script>
<script>
Benchmark.prototype.setup = function() {
    c.className = "foo";
    d.addClass("foo");
};

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 multiple
for (i = 0; i < 5; i++ ) {
  a.classList.add("foo" + i);
}
pending…
jQuery multiple
b.addClass("foo0 foo1 foo2 foo3 foo4");
pending…
classList single
arr = ("foo").split(" ");
for (i = arr.length; i >= 0; i--) {
    a.classList.add(arr[i]);
}
pending…
jQuery single
b.addClass("foo");
pending…
classList remove single
arr = ("foo").split(" ");
for (i = arr.length; i >= 0; i--) {
    if (a.classList.contains(arr[i])) a.classList.remove(arr[i]);
}
pending…
jquery remove single
b.removeClass("foo");
pending…
className single
a.className = "foo";
pending…
classList remove single check className
arr = ("foo").split(" ");
for (i = arr.length; i >= 0; i--) {
    if (a.className.indexOf(arr[i]) > -1) a.classList.remove(arr[i]);
}
pending…
classList remove single exists
arr = ("foo").split(" ");
for (i = arr.length; i >= 0; i--) {
    if (c.classList.contains(arr[i])) c.classList.remove(arr[i]);
}
pending…
jquery remove single exists
d.removeClass("foo");
pending…
classList remove single check className exists
arr = ("foo").split(" ");
for (i = arr.length; i >= 0; i--) {
    if (c.className.indexOf(arr[i]) > -1) c.classList.remove(arr[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