jquery.trim compared to string.trim
javascript, jquery, trim
Solution
JavaScript `.trim()` was defined in ES 5.1, and will not work for IE<9. Therefore, if you already use jQuery you could use the less performant `$.trim()`
jQuery's `$.trim()` Method does:
trim: function( text ) {
return text == null ? "" : ( text + "" ).replace( rtrim, "" );
}
where `rtrim` is basically this RegExp
new RegExp( "^" + whitespace + "+|((?:^|[^\\\\])(?:\\\\.)*)" + whitespace + "+$", "g" )
and `whitespace` is represented as this Group:
"[\\x20\\t\\r\\n\\f]"
In JavaScript (ES6) it's defined as:
21.1.3.25 String.prototype.trim ( )
This function interprets a String value as a sequence of UTF-16 encoded code points, as described in 6.1.4.
The following steps are taken: Let O be RequireObjectCoercible(this value). Let S be ToString(O). ReturnIfAbrupt(S). Let T be a String value that is a copy of S with both leading and trailing white space removed. The definition of white space is the union of WhiteSpace and LineTerminator. When determining whether a Unicode code point is in Unicode general category “Zs”, code unit sequences are interpreted as UTF-16 encoded code point sequences as specified in 6.1.4. Return T. NOTE The trim function is intentionally generic; it does not require that its this value be a String object. Therefore, it can be transferred to other kinds of objects for use as a method.
So let alone the implementation of JS's `.trim()` to browser vendors, the point is quite clear: Remove any representation of newlines or spaces from the beginning and end of a String.
Problem
Is there any difference between jQuery trim and native JavaScript one? Is there any other behaviour, safety, performance?