amir is wrong

JavaScript performance comparison

Test case created by ede edwardson

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<body class="is-search-panel-open">

</body>
<script>
Benchmark.prototype.setup = function() {
    function replace_long(oldS,newS,fullS) {
           var fullS = fullS != null ? fullS.toString() : '',
              i = 0;
       
           while (i < fullS.length) {
              if (fullS.substring && fullS.substring(i,i+oldS.length) == oldS) {
                 fullS = fullS.substring(0,i)+newS+fullS.substring(i+oldS.length,fullS.length);
                 i += newS.length;
              } else {
                 i++;
              }
           }
           return fullS;
        }
   
    function addNewPanelClassJS2(newPanelClass) {
      var fin = BODY.className;
      for (var i = panels.length - 1; i >= 0; i--) {
        fin = replace_long(fin, panels[i], ' ');
      }
      BODY.className = fin + ' ' + newPanelClass
    }
   
    function addNewPanelClassJS(newPanelClass) {
      BODY.className = BODY.className.replace(/\bis-[a-z]*-panel-open\b/i, ' ') + ' ' + newPanelClass;
    }
   
    function addNewPanelClassJQ(newPanelClass) {
     $(panels).each(function(c){
      $BODY.removeClass(c);
     });
     $BODY.addClass(newPanelClass);
    }
   
};
</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
jquery
var $BODY = $('body'), 
    panels = ['is-menu-panel-open','is-basket-panel-open','is-panel-open','is-help-panel-open','is-search-panel-open','is-account-panel-open', 'is-stores-panel-open'];

addNewPanelClassJQ('is-account-panel-open');
pending…
me
var BODY = $('body')[0]

addNewPanelClassJS('is-account-panel-open');
pending…
me 2
var BODY = $('body')[0],
    panels = ['is-menu-panel-open','is-basket-panel-open','is-panel-open','is-help-panel-open','is-search-panel-open','is-account-panel-open', 'is-stores-panel-open'];

addNewPanelClassJS2('is-account-panel-open');
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