Skip to content

Instantly share code, notes, and snippets.

@kindy61
Created December 1, 2009 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kindy61/246203 to your computer and use it in GitHub Desktop.
Save kindy61/246203 to your computer and use it in GitHub Desktop.
//trim vs
var t = 10, txt='\n adf '+document.body.innerHTML;
txt += txt + 'a \t';
console.time(1);
for (var i=0;i<t;i++){
(function( text ) {
return (text || "").replace( /^\s+|\s+$/g, "" );
})(txt);
}
console.timeEnd(1);
console.time(2);
for (var i=0;i<t;i++){
(function( text ) {
text = (text || "").replace( /^\s+/, "" );
var l = text.length - 1;
while(' \t\n\f'.indexOf(text.charAt(l)) > -1){
l-=1;
}
return text.substr(0, l+1);
})(txt);
}
console.timeEnd(2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment