multiline string

JavaScript performance comparison

Revision 14 of this test case created by eastkiki

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var s, S, A, X, Y, concat = String.prototype.concat;
  var MultiString = function(f) {
    return f.toString().split('\n').slice(1, -1).join('\n');
  };
  
  var multiLine = function (f) { 
    var s = f.toString(); 
    return s.slice(s.indexOf('/*') + 2, s.lastIndexOf('*/') - 2);
  }

};
</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
array init and set before join
A = new Array(9);
A[0] = 'aa';
A[1] = 'bb';
A[2] = 'cc';
A[3] = 'dd';
A[4] = 'ee';
A[5] = 'ff';
A[6] = 'gg';
A[7] = 'hh';
A[8] = 'ii';
A = A.join('\n');
pending…
similar to 3 but using new Array
Y = (new Array(
  'aa',
  'bb',
  'cc',
  'dd',
  'ee',
  'ff',
  'gg',
  'hh',
  'ii'
)).join('\n');
pending…
String.prototype.concat
concat.call(
  'aa\n',
  'bb\n',
  'cc\n',
  'dd\n',
  'ee\n',
  'ff\n',
  'gg\n',
  'hh\n',
  'ii\n');
pending…
''.concat
'aa\n'.concat(
  'bb\n',
  'cc\n',
  'dd\n',
  'ee\n',
  'ff\n',
  'gg\n',
  'hh\n',
  'ii\n');
pending…
one line string;
s = 'aa\nbb\ncc\ndd\nee\nff\ngg\nhh\nii';
pending…
array join concatenation (all at once)
X = [
  'aa',
  'bb',
  'cc',
  'dd',
  'ee',
  'ff',
  'gg',
  'hh',
  'ii'
].join('\n');
pending…
regular concatenation
s = '';
s += 'aa\n';
s += 'bb\n';
s += 'cc\n';
s += 'dd\n';
s += 'ee\n';
s += 'ff\n';
s += 'gg\n';
s += 'hh\n';
s += 'ii\n';
pending…
array join concatenation
S = [];
S.push('aa');
S.push('bb');
S.push('cc');
S.push('dd');
S.push('ee');
S.push('ff');
S.push('gg');
S.push('hh');
S.push('ii');
S = S.join('\n');
pending…
Slash concat
'aa\
bb\
cc\
dd\
ee\
ff\
gg\
hh\
ii';
pending…
regular 2
s = 'aa\n' +
  'bb\n' +
  'cc\n' +
  'dd\n' +
  'ee\n' +
  'ff\n' +
  'gg\n' +
  'hh\n' +
  'ii\n';
pending…
reduce
X = [
  'aa\n',
  'bb\n',
  'cc\n',
  'dd\n',
  'ee\n',
  'ff\n',
  'gg\n',
  'hh\n',
  'ii\n'
].reduce(function(a, b) {
  return a + b;
});
pending…
Use function toString
MultiString(function() {/**
aa
bb
cc
dd
ee
ff
gg
hh
ii
**/});
pending…
Use multiLine
multiLine(function() {/*
aa
bb
cc
dd
ee
ff
gg
hh
ii
*/});
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.

0 Comments