Different Formatter

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var item = '<script>try{parent.yfs_u1f({"m_open_close":"US Markets open in 7 hrs and 13 mins","m_local_time":"Wed, Jan  9, 2013, 2:17AM EST","VMW":{l10:"92.75",c10:"+0.73",p20:"+0.79"}});}catch(e){}</script>',curIndex,contiguousIndex,stringBuffer;
};
</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
Full REGEX
item.replace(/([{,])([^:]+):((["']).*?\4|)/g, function (match, p1, p2, p3, p4, offset) {
    if (p2[0] === "'" && p2[p2.length - 1] === "'") {
        p2 = '"' + p2.substring(1, p2.length - 1) + '"';
    }else if (p2[0] !== '"') {
        p2 = '"' + p2 + '"';
    }
    if (p3.length > 0 && p3[0] === "'" && p3[p3.length - 1] === "'") {
        p3 = '"' + p3.substring(1, p3.length - 1) + '"';
    }
    return p1 + p2 + ':' + p3;
});
pending…
Partials REGEX
curIndex = 0;
item.replace(/([{,])([^:]+)/g, function (match, p1, p2, offset) {
    var index;
    if (offset < curIndex) {
        return match;
    }
    if (p2[0] === "'" && p2[p2.length - 1] === "'") {
        p2 = '"' + p2.substring(1, p2.length - 1) + '"';
    }else if (p2[0] !== '"') {
        p2 = '"' + p2 + '"';
    }
    index = offset + match.length + 1;
    if (item[index] === '"') {
        do{
            index = item.indexOf('"', index+1);
        }while(item[index-1] === '\\');
    }
    curIndex = index;
    return p1 + p2;
});
pending…
indexOf loop
curIndex = 0;
contiguousIndex = 0;
stringBuffer = [];
while (curIndex < item.length) {
    switch (item[curIndex]) {
    case '{':
    case '}':
    case ':':
    case ',':
        curIndex += 1;
        break;
    case '"':
        index = curIndex;
        do {
            index = item.indexOf('"', index + 1);
        }while(item[index-1] === '\\');
        curIndex = index + 1;
        break;
    case "'":
        stringBuffer.push(item.substring(contiguousIndex, curIndex - 1));
        stringBuffer.push('"');
        index = curIndex;
        do {
            index = item.indexOf("'", index + 1);
        }while(item[index-1] === '\\');
        stringBuffer.push(item.substring(curIndex + 1, index - 1));
        stringBuffer.push('"');
        contiguousIndex = curIndex = index + 1;
        break;
    default:
        stringBuffer.push(item.substring(contiguousIndex, curIndex));
        stringBuffer.push('"');
        contiguousIndex = curIndex;
        index = item.indexOf(':', curIndex);
        contiguousIndex = item.indexOf('}', curIndex);
        if (contiguousIndex !== -1 && contiguousIndex < index) {
            index = contiguousIndex;
        }
        contiguousIndex = item.indexOf(',', curIndex);
        if (contiguousIndex !== -1 && contiguousIndex < index) {
            index = contiguousIndex;
        }
        stringBuffer.push(item.substring(curIndex, index));
        stringBuffer.push('"');
        contiguousIndex = curIndex = index;
        break;
    }
}
if (contiguousIndex < curIndex) {
    stringBuffer.push(item.substring(contiguousIndex));
}
stringBuffer.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