classcheckbefore replace

Benchmark created by rlemon on


Preparation HTML

<div id="myID"></div>
 
<script>
  function $(s, e) {
      if (!s) {
          return;
      }
      e = e || document;
      var r = e.querySelectorAll(s);
      return r.length === 1 ? r[0] : r;
  }
  Element.prototype.replaceClass = function(oC, nC) {
    if(this.hasClass(oC)) {
      this.className = this.className.replace(new RegExp("(\\b|^)" + oC + "(\\b|$)"), nC);
    }
        return this;
  };
  Element.prototype.replaceClass2 = function(oC, nC) {
  
      this.className = this.className.replace(new RegExp("(\\b|^)" + oC + "(\\b|$)"), nC);
  
        return this;
  };
  Element.prototype.hasClass = function(c) {
      return this.className.match(new RegExp("(\\b|^)" + c + "(\\b|$)")) !== null ? true : false;
  };
  var obj = $("#myID");
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
check
obj.replaceClass("one","two");
ready
nocheck
obj.replaceClass2("one","two");
ready

Revisions

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

  • Revision 1: published by rlemon on