remove classname

JavaScript performance comparison

Revision 2 of this test case created by VeganBen

Info

set to a known string

Preparation code

<div id="myDiv" class="class1 class2 class3">
</div>
<script>
Benchmark.prototype.setup = function() {
    var myDiv = document.getElementById('myDiv');
   
    function splitClass(el) {
      el.className = el.className.split(/\bclass2\b/).join('');
    }
   
    function replaceClass(el) {
      el.className = el.className.replace(/\bclass2\b/, '');
    }
   
    function classListRemove(el) {
      el.classList.remove('class2');
    }
   
    function setToKnownString(el) {
        el.className = "class1 class3"
    }
};
</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
Split
splitClass(myDiv);
pending…
Replace
replaceClass(myDiv);
pending…
classList
classListRemove(myDiv);
pending…
setToKnownString
setToKnownString(myDiv)
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