Trimming

Benchmark created on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
<script>
  var str = '                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed di diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo                                                                                                                                                                                                  ';
  
  function trim10 (str) {
        var whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
        for (var i = 0; i < str.length; i++) {
                if (whitespace.indexOf(str.charAt(i)) === -1) {
                        str = str.substring(i);
                        break;
                }
        }
        for (i = str.length - 1; i >= 0; i--) {
                if (whitespace.indexOf(str.charAt(i)) === -1) {
                        str = str.substring(0, i + 1);
                        break;
                }
        }
        return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
  }
  
  function trim11 (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;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Trim 1
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
ready
Trim 2
return str.replace(/^\s+/, '').replace(/\s+$/, '');
ready
Trim 3
return str.substring(Math.max(str.search(/\S/), 0), str.search(/\S\s*$/) + 1);
ready
Trim 4
return str.replace(/^\s+|\s+$/g, '');
ready
Trim 6
return str.replace(/^\s*(\S*(\s+\S+)*)\s*$/, '$1');
ready
Trim 7
return str.replace(/^\s*(\S*(?:\s+\S+)*)\s*$/, '$1');
ready
Trim 8
return str.replace(/^\s*((?:[\S\s]*\S)?)\s*$/, '$1');
ready
Trim 9
return str.replace(/^\s*([\S\s]*?)\s*$/, '$1');
ready
Trim 10
trim10(str);
ready
Trim 11
trim11(str);
ready
Trim 12
jQuery.trim(str);
ready

Revisions

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