trim()

Benchmark created by sofish on


Description

trim 性能

Preparation HTML

<input id="theinput" value="     dslakfjdls     dlskafjdslaf    ldsakfjdlskaf    lsdakfjdslka   sdlkafjdlska    sdlafkjdslkajf    "/>
<script>
  Function.prototype.method = function(name, func) {
   this.prototype[name] = func;
   return this;
  };
  
  String.method('trimReg', function() {
   return this.replace(/^\s+|\s+$/g, '');
  });
  
  String.method('trimSemiReg', function() {
   var str = this.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;
  });
  
  var r, v = document.getElementById('theinput').value;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
全正则
r = v.trimReg();
ready
半正则
r = v.trimSemiReg()
ready
native
r = v.trim()
ready

Revisions

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

  • Revision 1: published by sofish on
  • Revision 2: published by myf on