Various Replace Options (v2)

Revision 2 of this benchmark created by Muerl on


Preparation HTML

<script>
  var full = ('<ul data-role="listview" class="about-screen-list"> <li> <h1>Pyxis Server URL</h1> <p id="about-screen-url">,,</p> </li> <li> <h1>Web URL</h1> <p id="about-screen-web-url">,,</p> </li> <li> <h1>Authentication Type</h1> <p id="about-screen-auth-type">,,</p> </li> <li> <h1>Authentication Mode</h1> <p id="about-screen-auth-mode">,,</p> </li> <li> <h1>User ID</h1> <p id="about-screen-user-id">,,</p> </li> <li> <h1>Client Version</h1> <p id="about-screen-client-version">,,</p> </li> <li> <h1>Server Version</h1> <p id="about-screen-server-version">,,</p> </li> <li> <h1>Database Version</h1> <p id="about-screen-database-version">,,</p> </li> <li> <h1>Model</h1> <p id="about-screen-model">,</p> </li> <li> <h1>Browser</h1> <p id="about-screen-browser">,,</p> </li> <li> <h1>Last Server Response</h1> <p id="about-screen-last-response">,,</p> </li> <li> <h1>Last GPS Fix</h1> <p id="about-screen-last-gps">,,</p> </li> </ul>');
  var empty = '';
  var fullAlreadyEscaped = '&lt;ul data-role=&#34;listview&#34; class=&#34;about-screen-list&#34;&gt; &lt;li&gt; &lt;h1&gt;Pyxis Server URL&lt;/h1&gt; &lt;p id=&#34;about-screen-url&#34;&gt;,,&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;h1&gt;Web URL&lt;/h1&gt; &lt;p id=&#34;about-screen-web-url&#34;&gt;,,&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;h1&gt;Authentication Type&lt;/h1&gt; &lt;p id=&#34;about-screen-auth-type&#34;&gt;,,&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;h1&gt;Authentication Mode&lt;/h1&gt; &lt;p id=&#34;about-screen-auth-mode&#34;&gt;,,&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;h1&gt;User ID&lt;/h1&gt; &lt;p id=&#34;about-screen-user-id&#34;&gt;,,&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;h1&gt;Client Version&lt;/h1&gt; &lt;p id=&#34;about-screen-client-version&#34;&gt;,,&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;h1&gt;Server Version&lt;/h1&gt; &lt;p id=&#34;about-screen-server-version&#34;&gt;,,&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;h1&gt;Database Version&lt;/h1&gt; &lt;p id=&#34;about-screen-database-version&#34;&gt;,,&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;h1&gt;Model&lt;/h1&gt; &lt;p id=&#34;about-screen-model&#34;&gt;,&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;h1&gt;Browser&lt;/h1&gt; &lt;p id=&#34;about-screen-browser&#34;&gt;,,&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;h1&gt;Last Server Response&lt;/h1&gt; &lt;p id=&#34;about-screen-last-response&#34;&gt;,,&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;h1&gt;Last GPS Fix&lt;/h1&gt; &lt;p id=&#34;about-screen-last-gps&#34;&gt;,,&lt;/p&gt; &lt;/li&gt; &lt;/ul&gt;';
  lt = new RegExp('<', 'g');
  gt = new RegExp(">", 'g');
  q = new RegExp('"', 'g');
  dq = new RegExp("'", 'g');
  
  
  var fund2 = (function() {
  
   var div = document.createElement('div');
   var text = document.createTextNode('');
  
   div.appendChild(text);
  
   return function(str) {
    text.data = str;
    return div.innerHTML;
   };
  })();
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
dumbway
func = function(x) {
 x.split("<").join("&lt;").split(">").join("&gt;").split('"').join("&#34;").split("'").join("&#39;");
}
func(full);
func(empty);
func(fullAlreadyEscaped)
ready
myway
func = function(y) {
 if (y.indexOf('<') >= 0) y = y.replace(lt, "&lt;");
 if (y.indexOf(">") >= 0) y = y.replace(gt, "&gt;");
 if (y.indexOf('"') >= 0) y = y.replace(q, "&#34;");
 if (y.indexOf("'") >= 0) y = y.replace(dq, "&#39;")
}
func(full);
func(empty);
func(fullAlreadyEscaped)
ready
middle
func = function(x) {
 x.replace(lt, "&lt;").replace(gt, "&gt;").replace(dq, "&#34;").replace(q, "&#39;");
}

func(full);
func(empty);
func(fullAlreadyEscaped)
ready
try3
func = function(y) {
 if (y.indexOf('<') >= 0) y = y.split("<").join("&lt;");
 if (y.indexOf(">") >= 0) y = y.split(">").join("&gt;");
 if (y.indexOf('"') >= 0) y = y.split('"').join("&#34;");
 if (y.indexOf("'") >= 0) y = y.split("'").join("&#39;");
 return y;
}

func(full);
func(empty);
func(fullAlreadyEscaped)
ready
Crazy Hack
fund2(full);
fund2(empty);
fund2(fullAlreadyEscaped)
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.