regex cache

JavaScript performance comparison

Test case created

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
no cache
i18n = function(key){
    var val = key,
        args = Array.prototype.slice.call(arguments).slice(1),
        argsLen = args.length,
        i,
        j,
        choicePatterns = null,
        choicePatternMatch,
        paramIndex,
        choiceParamIntValue,
        choiceMatches;
    // First replace all normal occurencies - including those within choice patterns
    for (i=0; i<argsLen; i++) {
        val = val.replace('{' + i + '}', args[i]);
    }
    // ... then handle any remaining choice patterns such as {0,choice,0#0 rooms|1#1 room|1<{0} rooms|4<{0} rooms|10<{0} rooms}
    choicePatterns = val.match(/{\s*[0-9]+\s*,\s*choice,\s*[^}]+}/g);
    if (choicePatterns !== null) {
        for (i=0; i<choicePatterns.length; i++) {
            choicePatternMatch = choicePatterns[i].match(/{\s*([0-9]+)\s*,\s*choice,\s*([^}]+)}/);
            if (choicePatternMatch !== null) {
                paramIndex = parseInt(choicePatternMatch[1], 10);
                choiceParamIntValue = parseInt(args[paramIndex], 10);
                choiceMatches = null;
                if (choiceParamIntValue === 0) {
                    // 0
                    choiceMatches = choicePatternMatch[2].match(/\s*[0]#([^|}]+)/);
                } else if (choiceParamIntValue === 1) {
                    // 1
                    choiceMatches = choicePatternMatch[2].match(/\s*[1]#([^|}]+)/);
                } else if (choiceParamIntValue > 1 && choiceParamIntValue < 5) {
                    // 2-4
                    choiceMatches = choicePatternMatch[2].match(/\|\s*1<([^|}]+)/);
                } else if (choiceParamIntValue > 4 && choiceParamIntValue < 11) {
                    // 5-10
                    choiceMatches = choicePatternMatch[2].match(/\|\s*4<([^|}]+)/);
                } else if (choiceParamIntValue > 10) {
                    // 11 or more
                    choiceMatches = choicePatternMatch[2].match(/\|\s*10<([^|}]+)/);
                }
                if (choiceMatches !== null) {
                    val = val.replace(choicePatterns[i], choiceMatches[1]);
                }
            }    
        }
    }
    return val;
};

i18n('{0} {1,choice,0#0 Lorem0|1#1 Lorem1|1<{1} Lorem2-4|4<{1} Lorem5-10|10<{1} Lorem11+} dolor {1,choice,0#0 Lorem0|1#1 Lorem1|1<{1} Lorem2-4|4<{1} Lorem5-10|10<{1} Lorem11+}', '3', '3');
 
pending…
cache




var re1 = /\s*[0]#([^|}]+)/;
var re2 = /\s*[1]#([^|}]+)/;
var re3 = /\|\s*1<([^|}]+)/;
var re4 = /\|\s*4<([^|}]+)/;
var re5 = /\|\s*10<([^|}]+)/;


i18n = function(key){
    var val = key,
        args = Array.prototype.slice.call(arguments).slice(1),
        argsLen = args.length,
        i,
        j,
        choicePatterns = null,
        choicePatternMatch,
        paramIndex,
        choiceParamIntValue,
        choiceMatches;
    // First replace all normal occurencies - including those within choice patterns
    for (i=0; i<argsLen; i++) {
        val = val.replace('{' + i + '}', args[i]);
    }
    // ... then handle any remaining choice patterns such as {0,choice,0#0 rooms|1#1 room|1<{0} rooms|4<{0} rooms|10<{0} rooms}
    choicePatterns = val.match(/{\s*[0-9]+\s*,\s*choice,\s*[^}]+}/g);
    if (choicePatterns !== null) {
        for (i=0; i<choicePatterns.length; i++) {
            choicePatternMatch = choicePatterns[i].match(/{\s*([0-9]+)\s*,\s*choice,\s*([^}]+)}/);
            if (choicePatternMatch !== null) {
                paramIndex = parseInt(choicePatternMatch[1], 10);
                choiceParamIntValue = parseInt(args[paramIndex], 10);
                choiceMatches = null;
                if (choiceParamIntValue === 0) {
                    // 0
                    choiceMatches = choicePatternMatch[2].match(re1);
                } else if (choiceParamIntValue === 1) {
                    // 1
                    choiceMatches = choicePatternMatch[2].match(re2);
                } else if (choiceParamIntValue > 1 && choiceParamIntValue < 5) {
                    // 2-4
                    choiceMatches = choicePatternMatch[2].match(re3);
                } else if (choiceParamIntValue > 4 && choiceParamIntValue < 11) {
                    // 5-10
                    choiceMatches = choicePatternMatch[2].match(re4);
                } else if (choiceParamIntValue > 10) {
                    // 11 or more
                    choiceMatches = choicePatternMatch[2].match(re5);
                }
                if (choiceMatches !== null) {
                    val = val.replace(choicePatterns[i], choiceMatches[1]);
                }
            }    
        }
    }
    return val;
};

i18n('{0} {1,choice,0#0 Lorem0|1#1 Lorem1|1<{1} Lorem2-4|4<{1} Lorem5-10|10<{1} Lorem11+} dolor {1,choice,0#0 Lorem0|1#1 Lorem1|1<{1} Lorem2-4|4<{1} Lorem5-10|10<{1} Lorem11+}', '3', '3');
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