Mega Trim Test

JavaScript performance comparison

Revision 47 of this test case created by James Edward Lewis II

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var ws = ['[\x09-\x0D\x20\xA0\u1680\u180E\u2000-\u200A\u202F\u205F\u3000',
      '\u2028\u2029\uFEFF]+'].join('');
  /* Source of es5_shim(str): https://github.com/es-shims/es5-shim/blob/master/es5-shim.js#L1393 */
  var es5_shim = function () {

    'use strict';
    var trimBeginRegexp = new RegExp('^' + ws),
      trimEndRegexp = new RegExp(ws + '$');
    return function es5_shim(str) {
      return str.replace(trimBeginRegexp, '').replace(trimEndRegexp, '');
    };
  }();
  
  /* Source of es6_shim(str): https://github.com/es-shims/es6-shim/blob/master/es6-shim.js#L529 */
  var es6_shim = function () {

    'use strict';
    var trimRegexp = new RegExp(['^', ws, '|', ws, '$'].join(''), 'g');
    return function es6_shim(str) {
      return str.replace(trimRegexp, '');
    };
  }();
  
  function trim28(str) {

    'use strict';
    var l = str.length, i, j, c;
    for (i = 0; i < l; i++) {     
      c = str.charCodeAt(i);                   
      if (c < 8192) {
        if (c < 256 && (c === 32 || (c >= 9 && c <= 13) || c === 160)) continue;
        else if (c === 5760 || c === 6158) continue;
        break;
      } else if (c <= 8202 || c === 8232 || c === 8233 || c === 8239 || c === 8287 || c === 12288) continue;
      break;
    }
    for (j = l - 1; j >= i; j--) {
      c = str.charCodeAt(j);
      if (c < 8192) {
        if (c < 256 && (c === 32 || (c >= 9 && c <= 13) || c === 160)) continue;
        else if (c === 5760 || c === 6158) continue;
        break;
      } else if (c <= 8202 || c === 8232 || c === 8233 || c === 8239 || c === 8287 || c === 12288) continue;
      break;
    }
    return str.substring(i, j + 1);
  }
  
  function trim27a(str) {

    'use strict';
    var l = str.length, i, j, c;
    for (i = 0; i < l; i++) {
      c = str.charCodeAt(i);
      if (c === 32 || c === 10 || c === 13 || c === 9 || c === 12 || c === 11 || c === 160 || c === 5760 || c === 6158 || c === 8192 || c === 8193 || c === 8194 || c === 8195 || c === 8196 || c === 8197 || c === 8198 || c === 8199 || c === 8200 || c === 8201 || c === 8202 || c === 8232 || c === 8233 || c === 8239 || c === 8287 || c === 12288) continue;
      else break;
    }
    for (j = l - 1; j >= i; j--) {
      c = str.charCodeAt(j);
      if (c === 32 || c === 10 || c === 13 || c === 9 || c === 12 || c === 11 || c === 160 || c === 5760 || c === 6158 || c === 8192 || c === 8193 || c === 8194 || c === 8195 || c === 8196 || c === 8197 || c === 8198 || c === 8199 || c === 8200 || c === 8201 || c === 8202 || c === 8232 || c === 8233 || c === 8239 || c === 8287 || c === 12288) continue;
      else break;
    }
    return str.substring(i, j + 1);
  }
  
  function trim_native(str) {

    'use strict';
    return str.trim(); // should throw if no such function
  }
  
  var inp = '    \n\n \n\n   a \n a\n  a\n    a\n   a  \n\t\ta\t\ta\njahsdkjahkjshdakjhsdkahskd akhsjdh akjshd kashdkajhsd kajshd \nkauyiuqhwep iasldk qpwoie ad  \n   askdjaslkdjaslkjdaoiur qowioqwhr aspodiquw ijasod iqwiue pqowipoqiw epoqiwpeo iaslkjdqur \t kjahsdkj hakshd\nkajhdk\nk as d  \t\n'.split('\n'),
    buffer = new Array(inp.length),
    correct = ['', '', '', '', 'a', 'a', 'a', 'a', 'a', 'a		a',
      'jahsdkjahkjshdakjhsdkahskd akhsjdh akjshd kashdkajhsd kajshd',
      'kauyiuqhwep iasldk qpwoie ad', 'askdjaslkdjaslkjdaoiur qowioqwhr aspodiquw ijasod iqwiue pqowipoqiw epoqiwpeo iaslkjdqur 	 kjahsdkj hakshd', 'kajhdk', 'k as d', ''];
  
  function e1() {

    'use strict';
    for (var i = 0, len = inp.length; i < len; i++)
      buffer[i] = trim_native(inp[i]);
    return buffer;
  }
  
  function e2() {

    'use strict';
    for (var i = 0, len = inp.length; i < len; i++)
      buffer[i] = trim27a(inp[i]);
    return buffer;
  }
  
  function e3() {

    'use strict';
    for (var i = 0, len = inp.length; i < len; i++)
      buffer[i] = trim28(inp[i]);
    return buffer;
  }
  
  function e4() {

    'use strict';
    for (var i = 0, len = inp.length; i < len; i++)
      buffer[i] = es6_shim(inp[i]);
    return buffer;
  }
  
  function e5() {

    'use strict';
    for (var i = 0, len = inp.length; i < len; i++)
      buffer[i] = es5_shim(inp[i]);
    return buffer;
  }
  
  var l = 10000;
  while (l--) {
    e1();
    e2();
    e3();
    e4();
    e5();
  }

};

Benchmark.prototype.teardown = function() {
  for (var l = correct.length, errbuf = [], i = l - 1; i--;)
    if (a[i] !== correct[i])
      errbuf.push([a[i], correct[i]].join(' !== '));
  if (errbuf.length)
    throw new Error('Invalid Tests: ' + errbuf.join(' | '));

};
</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
trim-native
var a = e1();
pending…
trim27a
var a = e2();
pending…
trim28
var a = e3();
pending…
es6_shim
var a = e4();
pending…
es5_shim
var a = e5();
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