Mega trim test

JavaScript performance comparison

Revision 32 of this test case created

Info

Trying to find the fastest trim for IE6

Preparation code

<script>
  /*
Source of es5_shim(str): https://github.com/kriskowal/es5-shim/blob/master/es5-shim.js
*/

  var es5_shim = function(){
    var ws = "\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003" + "\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028" + "\u2029\uFEFF";
    ws = "[" + ws + "]";
    var trimBeginRegexp = new RegExp("^" + ws + ws + "*"),
        trimEndRegexp = new RegExp(ws + ws + "*$");
    return function(str) {
      return str.replace(trimBeginRegexp, "").replace(trimEndRegexp, "");
    };
  }();

  function trim27a(str) {
    var c;
    for (var i = 0; i < str.length; 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 (var j = str.length - 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 trim27(str) {
    var c;
    for (var i = 0; i < str.length; i++) {
      c = str.charCodeAt(i);
      if (c == 32 || c == 10 || c == 13 || c == 9 || c == 12) continue;
      else break;
    }
    for (var j = str.length - 1; j >= i; j--) {
      c = str.charCodeAt(j);
      if (c == 32 || c == 10 || c == 13 || c == 9 || c == 12) continue;
      else break;
    }
    return str.substring(i, j + 1);
  }

  function trim8(str) { return str.replace(/^\s*((?:[\S\s]*\S)?)\s*$/, '$1'); }

  function trim27b(str) {
    var l = str.length,
chars = {32:true,10:true,13:true,9:true,12:true};
    for (var i = 0; i < l; i++) {
      if (chars[str.charCodeAt(i)]) continue;
      break;
    }
    for (var j = l - 1; j >= i; j--) {
      if (chars[str.charCodeAt(i)]) continue;
      break;
    }
    return str.substring(i, j + 1);
  }

  var whiteSpaceCharacters = { 32: true, 10: true, 13: true, 9: true, 12: true };

  function trim27c(str) {
    var c;
    for (var i = 0; i < str.length; i++) {
      c = str.charCodeAt(i);
      if (whiteSpaceCharacters.hasOwnProperty(c)) continue;
      else break;
    }
    for (var j = str.length - 1; j >= i; j--) {
      c = str.charCodeAt(j);
      if (whiteSpaceCharacters.hasOwnProperty(c)) continue;
      else break;
    }
    return str.substring(i, j + 1);
  }

  function trim27d(str) {
    var c;
    for (var i = 0; i < str.length; i++) {
      c = str.charCodeAt(i);
      if (whiteSpaceCharacters[c]) continue;
      else break;
    }
    for (var j = str.length - 1; j >= i; j--) {
      c = str.charCodeAt(j);
      if (whiteSpaceCharacters[c]) continue;
      else break;
    }
    return str.substring(i, j + 1);
  }

  var whiteSpaceCharacterStrings = { " ": true, "\u000A": true, "\u000D": true, "\u0009": true, "\u000C": true };

  function trim27e(str) {
    var l = str.length;
    for (var i = 0; i < l; i++) {
      if (whiteSpaceCharacterStrings[str.charAt(i)]) continue;
      else break;
    }
    for (var j = l - 1; j >= i; j--) {
      if (whiteSpaceCharacterStrings[str.charAt(j)]) continue;
      else break;
    }
    return str.substring(i, j + 1);
  }

  var whiteSpaceCharacterArray = [];
  whiteSpaceCharacterArray[9] = true;
  whiteSpaceCharacterArray[10] = true;
  whiteSpaceCharacterArray[12] = true;
  whiteSpaceCharacterArray[13] = true;
  whiteSpaceCharacterArray[32] = true;
  function trim27f(str) {
    var l = str.length;
    for (var i = 0; i < l; i++) {
      if (whiteSpaceCharacterArray[str.charCodeAt(i)]) continue;
      else break;
    }
    for (var j = l - 1; j >= i; j--) {
      if (whiteSpaceCharacterArray[str.charCodeAt(j)]) continue;
      else break;
    }
    return str.substring(i, j + 1);
  }

  function trim27g(str) {
    var c;
    for (var i = 0, n = str.length; i < n; ++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;
      break;
    }
    for (var j = str.length - 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;
      break;
    }
    return str.substring(i, j + 1);
  }
  function trim27h(str) {
    var c;
    for (var i = 0, n = str.length; i < n; ++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 <= 8202) || c == 8232 || c == 8233 || c == 8239 || c == 8287 || c == 12288) continue;
      break;
    }
    for (var j = str.length - 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 <= 8202) || c == 8232 || c == 8233 || c == 8239 || c == 8287 || c == 12288) continue;
      break;
    }
    return str.substring(i, j + 1);
  }
  function trim27i(str) {
    var c;
    for (var i = 0, n = str.length; i < n; ++i) {
          c = str.charCodeAt(i);
          if (c <= 32 || c == 160 || c == 5760 || c == 6158 || (c >= 8192 && c <= 8202) || c == 8232 || c == 8233 || c == 8239 || c == 8287 || c == 12288) continue;
      break;
    }
    for (var j = str.length - 1; j >= i; --j) {
      c = str.charCodeAt(j);
          if (c <= 32 || c == 11 || c == 160 || c == 5760 || c == 6158 || (c >= 8192 && c <= 8202) || c == 8232 || c == 8233 || c == 8239 || c == 8287 || c == 12288) continue;
      break;
    }
    return str.substring(i, j + 1);
  }

  function trim_native(str) {
    return str.trim(); // should throw is 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');

  function e(f) {
    var x = [];
    for (i = 0; i < inp.length; i++) {
      x.push(f(inp[i]));
    }
    return x;
  }
</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
trim-native
e(trim_native);
pending…
trim27
e(trim27);
pending…
trim27a
e(trim27a);
pending…
es5_shim
e(es5_shim)
pending…
trim27b
e(trim27b);
pending…
trim27c
e(trim27c);
pending…
trim27d
e(trim27d);
pending…
trim8
e(trim8);
pending…
trim27e
e(trim27e);
pending…
trim27f
e(trim27f);
pending…
trim27g
e(trim27g);
pending…
trim27h
e(trim27h);
pending…
trim27i
e(trim27i);
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