StringEngine charAt

JavaScript performance comparison

Revision 2 of this test case created by Jonathan Perry

Preparation code

<script type="text/javascript">

        var FROMHTMLCODE = {

                        "&#38;": "\u0026"

                },
                FROMHTMLNAME = {

                        "&amp;": "\u0026"

                },
                X = /\&[\w]+\;|\&\#[\d]+\;|\&+/;

        function charAtA(i, s) {

                var x, m, v, c;

                if (typeof i === "number") {

                        (x = X).lastIndex = i;

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

                                if ((m = x.exec(s)) === null) {

                                        return s ;

                                } else if ((v = m.shift()).length === 1) {

                                        return v ;

                                }

                                return (typeof (c = FROMHTMLNAME[v]) === "string") ? c : (typeof (c = FROMHTMLCODE[v]) === "string") ? c : s ;

                        }

                }

                return null ;

        }

        function charAtB(i, s) {

                var x, m, v, c;

                if (typeof i === "number") {

                        (x = X).lastIndex = i;

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

                                return ((m = x.exec(s)) === null) ? s : ((v = m.shift()).length === 1) ? v : (typeof (c = FROMHTMLNAME[v]) === "string") ? c : (typeof (c = FROMHTMLCODE[v]) === "string") ? c : s ;

                        }

                }

                return null ;

        }

        function charAtC(i, s) {

                var x, m, v, c;

                if (typeof i === "number") {

                        (x = X).lastIndex = i;

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

                                return ((m = x.exec(s)) === null) ? s : ((v = m.shift()) === "&") ? v : (typeof (c = FROMHTMLNAME[v]) === "string") ? c : (typeof (c = FROMHTMLCODE[v]) === "string") ? c : s ;

                        }

                }

                return null ;

        }

        function charAtD(i, s) {

                var x, m, v, c;

                if (typeof i === "number") {

                        (x = X).lastIndex = i;

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

                                return ((m = x.exec(s)) === null) ? s : ((v = m.shift()) === "\u0026") ? v : (typeof (c = FROMHTMLNAME[v]) === "string") ? c : (typeof (c = FROMHTMLCODE[v]) === "string") ? c : s ;

                        }

                }

                return null ;

        }

</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
charAt A
s = charAtA(19, "something something & something something");
s = charAtA(19, "something something &amp; something something");
s = charAtA(19, "something something &#38; something something");
pending…
charAt B
s = charAtB(19, "something something & something something");
s = charAtB(19, "something something &amp; something something");
s = charAtB(19, "something something &#38; something something");
pending…
charAt C
s = charAtC(19, "something something & something something");
s = charAtC(19, "something something &amp; something something");
s = charAtC(19, "something something &#38; something something");
pending…
charAt D
s = charAtD(19, "something something & something something");
s = charAtD(19, "something something &amp; something something");
s = charAtD(19, "something something &#38; something something");
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