Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I made a trim-function speed in mind. This function beats in a clear difference all of 24 competitors (of which many use regular expressions) and also native string.trim() of Chrome and Chromium(!) and performs as speedy as Safari's trim(). Test results are here: <a href="http://jsperf.com/mega-trim-test/7" rel="nofollow">http://jsperf.com/mega-trim-test/7</a></p> <pre><code>function trim27(str) { var c; for (var i = 0; i &lt; str.length; i++) { c = str.charCodeAt(i); if (c == 32 || c == 10 || c == 13 || c == 9 || c == 12) continue; else break; } for (var j = str.length - 1; j &gt;= i; j--) { c = str.charCodeAt(j); if (c == 32 || c == 10 || c == 13 || c == 9 || c == 12) continue; else break; } return str.substring(i, j + 1); } </code></pre> <p>The function trims characters " \n\r\t\f", but it's easy to add more whitespace-characters, eg. those that regexp uses as whitespaces (\s) with only a minor performance lost ( please see <a href="http://jsperf.com/mega-trim-test/8" rel="nofollow">http://jsperf.com/mega-trim-test/8</a> ).</p> <p>Edit: The previous trim27() trims only the most common characters (" \n\r\t\f"), but to trim all possible whitespaces, I included below a new function mytrim(): </p> <pre><code>if (!String.prototype.trim || "\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF".trim() || navigator.userAgent.toString().toLowerCase().indexOf("chrome") != -1) var mytrim = function(str) { var c; for (var i = 0; i &lt; str.length; i++) { c = str.charCodeAt(i); if (c == 32 || c == 10 || c == 13 || c == 9 || c == 12 || c == 11 || c == 160 || c == 5760 || c == 6158 || c == 8192 || c == 8193 || c == 8194 || c == 8195 || c == 8196 || c == 8197 || c == 8198 || c == 8199 || c == 8200 || c == 8201 || c == 8202 || c == 8232 || c == 8233 || c == 8239 || c == 8287 || c == 12288 || c == 65279) continue; else break; } for (var j = str.length - 1; j &gt;= i; j--) { c = str.charCodeAt(j); if (c == 32 || c == 10 || c == 13 || c == 9 || c == 12 || c == 11 || c == 160 || c == 5760 || c == 6158 || c == 8192 || c == 8193 || c == 8194 || c == 8195 || c == 8196 || c == 8197 || c == 8198 || c == 8199 || c == 8200 || c == 8201 || c == 8202 || c == 8232 || c == 8233 || c == 8239 || c == 8287 || c == 12288 || c == 65279) continue; else break; } return str.substring(i, j + 1); }; else var mytrim = function(str) { return str.trim(); } </code></pre> <p>Use it this way:</p> <pre><code>var foo = mytrim(" \n \t Trimmed \f \n "); // foo is now "Trimmed" </code></pre> <p>The above mytrim() does the following: </p> <ul> <li>Trims 26 different whitespaces (all of 25 whitespaces mentioned in <a href="http://perfectionkills.com/whitespace-deviations/" rel="nofollow">http://perfectionkills.com/whitespace-deviations/</a> and additionally uFEFF, which is ZERO WIDTH NO-BREAK SPACE.</li> <li>Makes trimming results consistent across browsers. </li> <li>Uses native trim() if it is available AND has ability to trim all of 27 different whitespaces. The exception is Chrome and Chromium which both have so slow native trim() that instead of native we use our custom trim. </li> <li>AND THE MOST IMPORTANT: Is not beautiful and is not short, but IS CLEARLY FASTER than any of the 24 competitive alternatives in <a href="http://jsperf.com/mega-trim-test/12" rel="nofollow">http://jsperf.com/mega-trim-test/12</a> (exception: rather old Firefox 3.6.25 in Windows 7 runs mytrim() rather slowly for unknown reason).</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload