joinClasses: Args vs. Array

JavaScript performance comparison

Revision 6 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(' ');
    }
};
</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…

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