Skip to main content

Bug Tracker

Side navigation

Ticket #6216: jquery-optimized-trim.patch
File jquery-optimized-trim.patch, 0.8 KB (added by mgruner, March 04, 2010 08:43AM UTC)

Optimized trim function for jQuery

--- jquery-1.4.2-orig.js	2010-03-04 08:31:25.000000000 +0100
+++ jquery-1.4.2-optimized-trim.js	2010-03-04 09:25:59.000000000 +0100
@@ -44,7 +44,7 @@
 	rnotwhite = /\S/,
 
 	// Used for trimming whitespace
-	rtrim = /^(\s|\u00A0)+|(\s|\u00A0)+$/g,
+    rwhite = /\s|\u00A0/,
 
 	// Match a standalone tag
 	rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
@@ -585,7 +585,11 @@
 	},
 
 	trim: function( text ) {
-		return (text || "").replace( rtrim, "" );
+        var nonwhiteStartIndex = 0,
+            nonwhiteEndIndex = text.length;
+        while (rwhite.test(text.charAt(nonwhiteStartIndex++)));
+        while (rwhite.test(text.charAt(--nonwhiteEndIndex)));
+        return text.slice(--nonwhiteStartIndex, ++nonwhiteEndIndex);
 	},
 
 	// results is for internal usage only

Download in other formats:

Original Format