regex vs. jQuery filter

JavaScript performance comparison

Revision 2 of this test case created

Info

This testcase compares the performance of a particular selection of elements. Once using a regular expression and once using jQuery .filter()

Preparation code

<a href="/Recipe/">Receipe</a>
<br>
<a href="/test/">Receipe</a>
<br>
<a href="/Product 1/">Receipe</a>
<br>
<a href="/asdf/Recipe">Receipe</a>
<br>
<a href="/Recipe/afdsa/">Receipe</a>
<br>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var itemFilter = 'Recipe|Product 1|test';
    var filterStr = "\/" + String(itemFilter).replace(/\|/g, "\/|\/") + "\/"
    var filter = RegExp(filterStr);
    var itemFilter1 = 'Recipe|Product'.split('|');
    var disablelink;
    var filter1 = RegExp(filterStr.replace(/ /g,'%20'));
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
.filter()
$('a').each(function() {                                        
                                        if(filter1.test(this.href)) {
                                                $(this).removeAttr("href");        
                                        }
                        });
pending…
regex
$('a').each(function() {                                        
                                        if(filter.test(this.href.replace(/%20/g, ' '))) {
                                                $(this).removeAttr("href");        
                                        }
                        });
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