String concat function vs inline

JavaScript performance comparison

Test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var base = "so many ways to skin a cat",
      c = 0, bl, hbl,
      rand = function( s, e ) {
          return Math.floor( Math.random() * ( e - s ) ) + s;
      },
      concatenateThese = function() {
          return [].slice.call( arguments ).join( "" );
      };
  while ( ++c < 10 ) {
      base += base;
  }
  bl = base.length;
  hbl = Math.floor( bl / 2 );
  c = 0;
  while ( ++c < 100 ) {
      window[ "randStr" + c ] = base.substr( rand( 0, hbl ), rand( hbl, bl ) );
  }

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Simple Inline Concatenation
var rslt = window[ "randStr" + rand( 0, 100 ) ] +
    window[ "randStr" + rand( 0, 100 ) ] +
    window[ "randStr" + rand( 0, 100 ) ] +
    window[ "randStr" + rand( 0, 100 ) ] +
    window[ "randStr" + rand( 0, 100 ) ] +
    window[ "randStr" + rand( 0, 100 ) ] +
    window[ "randStr" + rand( 0, 100 ) ];
pending…
Inline arr.join()
var rslt = [ window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ] ].join( "" );
pending…
arr.join() Function Handling an Arbitrary Number of Args
var rslt = concatenateThese( window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ] );
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