joinClasses: Args vs. Array

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function joinClassesArgs() {
      var className = '';
      if (arguments.length > 0) {
        className = arguments[0];
        for (var ii = 1; ii < arguments.length; ii++) {
          className += ' ' + arguments[ii];
        }
      }
      return className;
    }
    function joinClassesArray(classes) {
      var className = '';
      if (classes.length > 0) {
        className = classes[0];
        for (var ii = 1; ii < classes.length; ii++) {
          className += ' ' + classes[ii];
        }
      }
      return className;
    }
    function joinClassesArrayJoin(classes) {
      return classes.join(' ');
    }
    function joinClassesManual(arg0, arg1, arg2, arg3) {
      var className = arg0 || '';
      if (!arg0) return className;
      if (!arg1) return className;
      className += ' ' + arg1;
      if (!arg2) return className;
      className += ' ' + arg2;
      if (!arg3) return className;
      className += ' ' + arg3;
      return className;
    }
};
</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…
Array
if (joinClassesArray(['foo', 'bar']) !== 'foo bar') {
  throw new Error('Wrong!');
}
pending…
Array (Join)
if (joinClassesArrayJoin(['foo', 'bar']) !== 'foo bar') {
  throw new Error('Wrong!');
}
pending…
Args (Manual)
if (joinClassesManual('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