regex replace vs jQuery .text()

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var html_string = '<p><a href="http://cdn.androidpolice.com/wp-content/uploads/2013/05/nexusae0_124.png"><img title="1" style="display: inline" alt="1" src="http://cdn.androidpolice.com/wp-content/uploads/2013/05/nexusae0_1_thumb23.png" width="150" height="150"/></a></p> <p><a href="http://www.androidpolice.com/2013/05/06/htc-first-review-the-phone-you-didnt-realize-youd-actually-want/" target="_blank">When I reviewed the First</a>, I realized it was much, <em>much</em> more than just a <em>Facebook experience device.</em> Sure, it\'s sporting Home out of the box, flashes a Facebook logo during the boot process, and is adorned with that same logo on the back, but it\'s not <em>just </em>about Facebook. This little diamond in the rough is running stock Android 4.1.2 beneath Facebook Home, so you\'re quite literally three (or so) taps away from a Nexus-like experience. </p> <p>No quicker than I could say good things about this device, however, people were trying to shoot it down, with the price playing a pretty large factor in that.<div class=clear></div> <a href="http://www.androidpolice.com/2013/05/08/att-drops-the-price-of-the-htc-first-to-0-99-probably-the-best-damn-phone-youll-find-for-a-dollar/" class="read_more"><div class=excerpt-end><img src=http://www.androidpolice.com/wp-content/themes/ap1/images/read_on.png /></div></a></p><div style=\'clear: both\'><b>Done With This Post? You Might Also Like These:</b></div><ul style=\'list-style: inside\'><li><a href="http://www.androidpolice.com/2011/02/03/dell-venue-on-sale-now-for-500-from-dell-shipping-february-18th/" rel="bookmark" title="February 3, 2011">Dell Venue On Sale Now For $500 From Dell; Shipping February 18th</a></li> <li><a href="http://www.androidpolice.com/2011/01/27/amazon-order-page-for-the-motorola-atrix-leaks-150-on-contract-600-off/" rel="bookmark" title="January 27, 2011">Amazon Accidentally Leaks Pricing Of Motorola Atrix 4G ($150/$600) And HTC Inspire 4G ($100/$500)</a></li> <li><a href="http://www.androidpolice.com/2011/11/24/off-contract-virgin-mobile-wildfire-s-now-on-sale-for-just-100/" rel="bookmark" title="November 24, 2011">Off-Contract Virgin Mobile USA Wildfire S On Sale For $99.99 (Half Off) Till November 26th</a></li> <li><a href="http://www.androidpolice.com/2013/04/06/video-this-is-how-facebook-will-advertise-home-and-the-htc-first/" rel="bookmark" title="April 6, 2013">Video: This Is How Facebook Will Advertise Home And The HTC First</a></li> </ul><!-- Similar Posts took 0.858 ms (cached) --><p><a href=\'http://www.androidpolice.com/2013/05/08/att-drops-the-price-of-the-htc-first-to-0-99-probably-the-best-damn-phone-youll-find-for-a-dollar/\'>AT&#38;T Drops The Price Of The HTC First To $0.99, Probably The Best Damn Phone You\'ll Find For A Dollar</a> was written by the awesome team at <a href=\'http://www.androidpolice.com\'>Android Police</a>.</p><img width=\'1\' height=\'1\' src=\'http://rss.feedsportal.com/c/33941/f/615677/p/1/s/33d8def2/mf.gif\' border=\'0\'/><div class=\'mf-viral\'><table border=\'0\'><tr><td valign=\'middle\'><a href="http://share.feedsportal.com/share/twitter/?u=http%3A%2F%2Fwww.androidpolice.com%2F2013%2F05%2F08%2Fatt-drops-the-price-of-the-htc-first-to-0-99-probably-the-best-damn-phone-youll-find-for-a-dollar%2F&t=AT%26T+Drops+The+Price+Of+The+HTC+First+To+%240.99%2C+Probably+The+Best+Damn+Phone+You%27ll+Find+For+A+Dollar" target="_blank"><img src="http://res3.feedsportal.com/social/twitter.png" border="0" /></a>&nbsp;<a href="http://share.feedsportal.com/share/facebook/?u=http%3A%2F%2Fwww.androidpolice.com%2F2013%2F05%2F08%2Fatt-drops-the-price-of-the-htc-first-to-0-99-probably-the-best-damn-phone-youll-find-for-a-dollar%2F&t=AT%26T+Drops+The+Price+Of+The+HTC+First+To+%240.99%2C+Probably+The+Best+Damn+Phone+You%27ll+Find+For+A+Dollar" target="_blank"><img src="http://res3.feedsportal.com/social/facebook.png" border="0" /></a>&nbsp;<a href="http://share.feedsportal.com/share/linkedin/?u=http%3A%2F%2Fwww.androidpolice.com%2F2013%2F05%2F08%2Fatt-drops-the-price-of-the-htc-first-to-0-99-probably-the-best-damn-phone-youll-find-for-a-dollar%2F&t=AT%26T+Drops+The+Price+Of+The+HTC+First+To+%240.99%2C+Probably+The+Best+Damn+Phone+You%27ll+Find+For+A+Dollar" target="_blank"><img src="http://res3.feedsportal.com/social/linkedin.png" border="0" /></a>&nbsp;<a href="http://share.feedsportal.com/share/gplus/?u=http%3A%2F%2Fwww.androidpolice.com%2F2013%2F05%2F08%2Fatt-drops-the-price-of-the-htc-first-to-0-99-probably-the-best-damn-phone-youll-find-for-a-dollar%2F&t=AT%26T+Drops+The+Price+Of+The+HTC+First+To+%240.99%2C+Probably+The+Best+Damn+Phone+You%27ll+Find+For+A+Dollar" target="_blank"><img src="http://res3.feedsportal.com/social/googleplus.png" border="0" /></a>&nbsp;<a href="http://share.feedsportal.com/share/email/?u=http%3A%2F%2Fwww.androidpolice.com%2F2013%2F05%2F08%2Fatt-drops-the-price-of-the-htc-first-to-0-99-probably-the-best-damn-phone-youll-find-for-a-dollar%2F&t=AT%26T+Drops+The+Price+Of+The+HTC+First+To+%240.99%2C+Probably+The+Best+Damn+Phone+You%27ll+Find+For+A+Dollar" target="_blank"><img src="http://res3.feedsportal.com/social/email.png" border="0" /></a></td><td valign=\'middle\'></td></tr></table></div><br/><br/><a href="http://da.feedsportal.com/r/869850866/u/49[MFV2]PARTNER[MFV2]/f/615677/c/33941/s/869850866/a2.htm"><img src="http://da.feedsportal.com/r/869850866/u/49[MFV2]PARTNER[MFV2]/f/615677/c/33941/s/869850866/a2.img" border="0"/></a><img width="1" height="1" src="http://pi.feedsportal.com/r/869850866/u/49[MFV2]PARTNER[MFV2]/f/615677/c/33941/s/869850866/a2t.img" border="0"/><div class="feedflare"><a href="http://feeds.feedburner.com/~ff/AndroidPolice?a=5HS1Ux1_QII:Kqj3CMIBhdE:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/AndroidPolice?i=5HS1Ux1_QII:Kqj3CMIBhdE:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AndroidPolice?a=5HS1Ux1_QII:Kqj3CMIBhdE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/AndroidPolice?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AndroidPolice?a=5HS1Ux1_QII:Kqj3CMIBhdE:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/AndroidPolice?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AndroidPolice?a=5HS1Ux1_QII:Kqj3CMIBhdE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/AndroidPolice?i=5HS1Ux1_QII:Kqj3CMIBhdE:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AndroidPolice?a=5HS1Ux1_QII:Kqj3CMIBhdE:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/AndroidPolice?i=5HS1Ux1_QII:Kqj3CMIBhdE:-BTjWOF_DHI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AndroidPolice?a=5HS1Ux1_QII:Kqj3CMIBhdE:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/AndroidPolice?i=5HS1Ux1_QII:Kqj3CMIBhdE:F7zBnMyn0Lo" border="0"></img></a></div><img src="http://feeds.feedburner.com/~r/AndroidPolice/~4/5HS1Ux1_QII" height="1" width="1"/>'
   
    var regex = /(<([^>]+)>)/ig;
    var regex2 = /<(.|\n)*?>/gm;
    var regex3 = /<(?:.|\n)*?>/gm;
    var regex4 = /<(?:.|\n)*?>/ig;
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
regex replace
html_string.replace(regex, "");
pending…
jQuery .text()
$(html_string).text();
pending…
Regex 2
html_string.replace(regex2, "");
pending…
regex 3
html_string.replace(regex3, "");
pending…
regex 4
html_string.replace(regex4, '');
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment