Faster trim()

Faster trim()


When testing performance of several JavaScript libraries, I came
across an unexpected result with the trim() method.
The jQuery library (as well as several others) trim using a single reg
exp replace.
(text || "").replace( /^\s+|\s+$/g, "" )
This seems reasonable. Why process the string twice when once will do?
However, using two reg expressions is signifanctly faster. That is,
each regular expression is very simple (no '|') and therefore runs
much faster, making up for processing the string twice.
(text ? text.replace(/^\s+/g,"").replace(/\s+$/g,"") : "")
Timing was done using Firebug (Firebug Lite for IE) on a Windows XP
machine w/ Firebug 2 and IE 7.
The test page is included here.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>JavaScript Library Test of 'trim'</title>
<script type="text/javascript" src="/jsunit/app/jsUnitCore.js"></
script>
<script type="text/javascript" src="/firebug/firebug.js"></
script>
<script type="text/javascript" src="/jquery-1.2.1.js"></script>
<script type="text/javascript">
<!--
function init()
{
if (console.open) console.open();
console.log("Initialized");
timeTrim("jQuery");
timeTrim("ektron");
}
function timeTrim(name)
{
var testloop = 1000;
var sNoSpace = "Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.";
var sEndSpace = "Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum. ";
var sBothSpace = " Lorem ipsum dolor sit amet,
consectetur adipisicing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est
laborum. ";
var s0 = "";
var s1 = "";
var s2 = "";
console.time("trim: " + name);
switch (name)
{
case "jQuery":
for (var i = 0; i < testloop; i++)
{
s0 = $.trim(sNoSpace);
s1 = $.trim(sEndSpace);
s2 = $.trim(sBothSpace);
}
break;
case "ektron":
for (var i = 0; i < testloop; i++)
{
s0 = sNoSpace.replace(/^\s+/,"").replace(/\s+
$/,"");
s1 = sEndSpace.replace(/^\s+/,"").replace(/\s+
$/,"");
s2 = sBothSpace.replace(/^\s+/,"").replace(/\s+
$/,"");
}
break;
}
console.timeEnd("trim: " + name);
if (s1 != s0) console.error(name + ": trim() failed w/
trailing space.");
if (s2 != s0) console.error(name + ": trim() failed w/
spaces on both ends.");
}
// -->
</script>
</head>
<body>
<h2>JavaScript Library Test of 'trim'</h2>
<script type="text/javascript">
<!--
jQuery(init);
// -->
</script>
</body>
</html>