Caching vs RegExp Literal

JavaScript performance comparison

Test case created by Aadit M Shah

Info

Does storing regular expressions in a variable help?

Preparation code

<script>var expression = /World/;</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
RegExp
"Hello World!".replace(/World/, function () {
    return "JavaScript";
});
pending…
Cached RegExp
"Hello World!".replace(expression, function () {
    return "JavaScript";
});
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