trim()
JavaScript performance comparison
Info
trim 性能
Preparation code
<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>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
全正则 |
|
pending… |
半正则 |
|
pending… |
native |
|
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:
- Revision 1: published by sofish
- Revision 2: published by myf
0 comments