Cache RegExp or new exp

JavaScript performance comparison

Test case created by Ben

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var regex = new RegExp(/^(contact|billing|delivery)_(company_name|name|lastname|email|acode_home|acode_work|phone_home|phone_work|phone_mobile|address_1|address_2|zip_suburb|suburb|state|zip|country)/);
    var regex2 = /^(contact|billing|delivery)_(company_name|name|lastname|email|acode_home|acode_work|phone_home|phone_work|phone_mobile|address_1|address_2|zip_suburb|suburb|state|zip|country)/;
};
</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
Cache RegExp
var string = 'contact_name';
string.replace(regex, '$2');
pending…
New each time
var string = 'contact_name';
string.replace(/^(contact|billing|delivery)_(company_name|name|lastname|email|acode_home|acode_work|phone_home|phone_work|phone_mobile|address_1|address_2|zip_suburb|suburb|state|zip|country)/, '$2')
pending…
No new Regexp
var string = 'contact_name';
string.replace(regex2, '$2')
pending…

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

Compare results of other browsers

0 comments

Add a comment