Remove Class Via Object Prototype

JavaScript performance comparison

Test case created by VisioN

Preparation code

<div class="test">
  Class Name: test
</div>
<div class="test">
  Class Name: test
</div>
<div class="test">
  Class Name: test
</div>
<div class="test">
  Class Name: test
</div>
<div class="test">
  Class Name: test
</div>
<div class="test">
  Class Name: test
</div>
<div class="test">
  Class Name: test
</div>
<script>
Benchmark.prototype.setup = function() {
    Object.prototype.removeClassReverseFor = function(class_name) {
      if (this.length) {
        for (var i = this.length - 1; i >= 0; i--) {
          this[i].classList.remove(class_name);
        }
      }
    }
   
    Object.prototype.removeClassWhile = function(class_name) {
      if (this.length) {
        while (this.length > 0) {
          console.log(this.length);
          this[0].classList.remove(class_name);
        }
      }
    }
   
    Object.prototype.removeClassSlice = function(class_name) {
      var that = Array.prototype.slice.call(this);
      for (var i = 0; i < that.length; i++) {
        that[i].classList.remove(class_name);
      }
    }
};
</script>

Preparation code output

Class Name: test
Class Name: test
Class Name: test
Class Name: test
Class Name: test
Class Name: test
Class Name: 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
Reverse For Loop
document.getElementsByClassName("test").removeClassReverseFor("test");
pending…
While Loop
document.getElementsByClassName("test").removeClassWhile("test");
pending…
Array Slice For Loop
document.getElementsByClassName("test").removeClassSlice("test");
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment