New fhfhfhf

JavaScript performance comparison

Revision 18 of this test case created

Info

A solution for replacement of strings.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var string = 'The hexadecimal color code <strong><code>{hex}</code></strong> is a {shadeLightness} shade of <strong>{shade}</strong>. In the RGB color model <code>{hex}</code> is comprised of {red}% red, {green}% green and {blue}% blue. In the HSL color space <em><code>{hex}</code></em> has a hue of {hue} degrees, {saturation}% saturation and {lightness}% lightness. This color has an <em>approximate</em> wavelength of <strong>{wavelength} nm</strong>.';
};
</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
.replace(/\./g,' ')
result = string.replace(/\{hex\}/g, '#0066cc');
pending…
String
/**
 * ReplaceAll by Fagner Brack (MIT Licensed)
 * Replaces all occurrences of a substring in a string
 */

String.prototype.replaceAll = function( token, newToken, ignoreCase ) {
    var _token;
    var str = this + "";
    var i = -1;

    if ( typeof token === "string" ) {

        if ( ignoreCase ) {

            _token = token.toLowerCase();

            while( (
                i = str.toLowerCase().indexOf(
                    token, i >= 0 ? i + newToken.length : 0
                ) ) !== -1
            ) {
                str = str.substring( 0, i ) +
                    newToken +
                    str.substring( i + token.length );
            }

        } else {
            return this.split( token ).join( newToken );
        }

    }
return str;
};

string.replaceAll('{hex}', '#0066cc');
pending…
.split('.').join(' ')
result = mystring.split('.').join(' ');
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