regex vs indexOf replace

JavaScript performance comparison

Test case created by ThinkingStiff

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function replaceCountryCodeIndexOf ( number, replaceWith ) {
        return replaceWith + number.substr( number.indexOf( ' ' ) );
    };
   
    function replaceCountryCodeRegEx ( number, replaceWith ) {
        return number.replace(/^(\+\d*)/, replaceWith );
    };
   
    var number = '',
        html = '';
};
</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
number = '+90 (533) 333 33 33';
html = replaceCountryCodeRegEx( number, 'Turkey' );
number = '+880 (533) 333 33 33';
html = replaceCountryCodeRegEx( number, 'Bangladesh' );
 
pending…
indexOf
number = '+90 (533) 333 33 33';
html = replaceCountryCodeIndexOf( number, 'Turkey' );
number = '+880 (533) 333 33 33';
html = replaceCountryCodeIndexOf( number, 'Bangladesh' );
 
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