ClassList versus ClassName

JavaScript performance comparison

Revision 17 of this test case created by

Preparation code

<div id="foo"></div>
<script>fooEl = document.getElementById('foo');</script>

      
<script>
Benchmark.prototype.setup = function() {
  fooEl.className = ' aaaa bbbb cccc dddd '; 
  //note the leading and trailing spaces

};
</script>

Preparation code output

<div id="foo"></div>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
classList.remove
fooEl.classList.remove('aaaa');
pending…
className.replace
fooEl.className = fooEl.className.replace('aaaa ', ' ');
pending…
classList.add
if( fooEl.className.indexOf('zzzz') === -1 ){
fooEl.classList.add('zzzz');
}
pending…
className +=
if( fooEl.className.indexOf(' zzzz') === -1 ){
    fooEl.className += ' zzzz';
}
pending…
remove
if( fooEl.className.indexOf('zzzz') !== -1 ){
    fooEl.classList.remove('zzzz');
}
pending…
replace
if( fooEl.className.indexOf(' zzzz') !== -1 ){
    fooEl.className = fooEl.className.replace(' zzzz', '');
}
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.

0 Comments