Mega trim test (v16)

Revision 16 of this benchmark created by Timo on


Description

Trim function

Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
  // http://stackoverflow.com/a/196926/1691517
  var Pat = function(str) {
      return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
      }
      
      
      
      // http://stackoverflow.com/a/249412/1691517
      
  var jquery_trim = function(str) {
      return $.trim(str);
      }
      
      
      
      // http://blog.stevenlevithan.com/archives/faster-trim-javascript
      
  var trim11 = function(str) {
      str = str.replace(/^\s+/, '');
      for (var i = str.length - 1; i >= 0; i--) {
        if (/\S/.test(str.charAt(i))) {
          str = str.substring(0, i + 1);
          break;
        }
      }
      return str;
      }
      
      
      /*
mytrim() uses native function if it is available and can trim all required whitespace chars, otherwise it uses custom function.
because of slow native trim of chrome and chromium, custom function is used in them.
*/
      
      
  if (!String.prototype.trim || "\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".trim() || navigator.userAgent.toString().toLowerCase().indexOf("chrome") != -1) var mytrim = function(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 || c == 65279) 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 || c == 65279) continue;
        else break;
      }
      return str.substring(i, j + 1);
      };
  else var mytrim = function(str) {
      return str.trim();
      }
      
      
      
  var mytrim_plain = function(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 || c == 65279) 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 || c == 65279) continue;
        else 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

Ready to run.

Testing in
TestOps/sec
trim-native
e(trim_native);
ready
Pat
e(Pat);
ready
trim11
e(trim11);
ready
mytrim
e(mytrim);
ready
mytrim_plain
e(mytrim_plain);
ready
jquery_trim
e(jquery_trim);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.