joinClasses: Args vs. Array

JavaScript performance comparison

Revision 8 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function joinClassesArgs(className) {
      if (!className) {
        className = '';
      }
      var nextClass;
      var argLength = arguments.length;
      if (argLength > 1) {
        for (var ii = 1; ii < argLength; ii++) {
          nextClass = arguments[ii];
          nextClass && (className += ' ' + nextClass);
        }
      }
      return className;
    }
   
   
    function joinClassesFilter() {var classes=Array.prototype.slice.call(arguments,0);
      return classes.filter(function(className)  {return !!className;}).join(' ');
    }
   
    function joinClassesFilter2() {var classes=Array.prototype.slice.call(arguments,0);
      return classes.filter(Boolean).join(' ');
    }
   
    function joinClassesFilter3(className1, className2) {
      if (!className1) {
        if (!className2) {
          return '';
        } else {
          return className2;
        }
      } else if (!className2) {
        return className1;
      } else {
        return className1 + ' ' + className2;
      }
    }
   
    function joinClassesFilter5(className1, className2) {
      if (className1 && className2) {
        return className1 + ' ' + className2;
      } else if (className1) {
        return className1;
      } else if (className2) {
        return className2;
      } else {
        return '';
      }
    }
   
    function joinClassesFilter4(className1, className2) {
      return (className1 ? className1 : '') + (className1 && className2 ? ' ' : '') + (className2 ? className2 : '');
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Args
if (joinClassesArgs('foo', 'bar') !== 'foo bar') {
  throw new Error('Wrong!');
}
pending…
Arguments + Filter
if (joinClassesFilter('foo', 'bar') !== 'foo bar') {
  throw new Error('Wrong!');
}
pending…
Arguments 2
if (joinClassesFilter2('foo', 'bar') !== 'foo bar') {
  throw new Error('Wrong!');
}
pending…
Arguments 3
if (joinClassesFilter3('foo', 'bar') !== 'foo bar') {
  throw new Error('Wrong!');
}
pending…
Arguments 4
if (joinClassesFilter4('foo', 'bar') !== 'foo bar') {
  throw new Error('Wrong!');
}
pending…
Arguments 5
if (joinClassesFilter5('foo', 'bar') !== 'foo bar') {
  throw new Error('Wrong!');
}
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