jquery.trim vs string.prototype.trim (v11)

Revision 11 of this benchmark created on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
  function trim12(str) {
   var str = str.replace(/^\s\s*/, ''),
       ws = /\s/,
       i = str.length;
   while (ws.test(str.charAt(--i)));
   return str.slice(0, i + 1);
  }
  
  var stringTrim = String.prototype.trim;
String.prototype.trimNative = function(){
    return this.replace(/^\s*/, '').replace(/\s*$/, '');
}

String.prototype.otherTrim = function(){
    return this.replace(/^\s+|\s+$/g, ''); 
}
</script>

Setup

var test = new String("      hello world          ");

Test runner

Ready to run.

Testing in
TestOps/sec
jquery.trim 1.9.1
$.trim(test)
ready
String.prototype.trim
test.trim()
ready
native String.prototype
test.trimNative()
ready
Another Regex
test.otherTrim()
ready

Revisions

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