for performance, readability, and consistency, one should always use the native syntax.
Its actually been several years now that String.prototoype.trim() has been bundled with browsers.
if you need backwards-compatibility (aka graceful degradation) use a ployfill to make sure the String method is the same as it is in all current versions of all major browsers:
in the case of trim(), we can do it 100% backwards compatible, so there's no reason to have to balance an extra pair of parens when the proto is so handy:
Code:
"".trim|| (String.prototype.trim = function trim() {
return this.replace(/^\s+|\s+$/g,"");
});
if you already hard-coded trim(str) into your code, you can speed up your flow by re-binding the native:
Code:
var trim=eval.call.bind("".trim);
//example usage:
trim( " hello ");