StringEngine charAt

JavaScript performance comparison

Revision 7 of this test case created by Jonathan Perry

Preparation code

<script type="text/javascript">

        var FROMHTMLCODE = {

                        "&#38;": "\u0026"

                },
                FROMHTMLNAME = {

                        "&amp;": "\u0026"

                },
                X = /\u0026[\w]+\u003b|\u0026\u0023[\d]+\u003b|\u0026+/,
A = "\u0026";

        function charAtA(i, s) {

                var v, a = A, x = X, m, c;

                if (typeof i === "number" && typeof s === "string") {

                        v = s.charAt(i);

                        if (v === a) {

                                x.lastIndex = i;

                                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 ;

                        } else {

                                return v;

                        }

                }

                return null ;

        }

        function charAtB(i, s) {

                var v, x, a, m, c;

                if (typeof i === "number" && typeof s === "string") {

                        v = s.charAt(i);
                        a = A;

                        if (v === a) {

                                x = X;
                                x.lastIndex = i;

                                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 ;

                        } else {

                                return v;

                        }

                }

                return null ;

        }

        function charAtC(i, s) {

                var v, x, a, m, c;

                if (typeof i === "number" && typeof s === "string") {

                        v = s.charAt(i);
                        a = A;

                        if (v === a) {

                                x = X;
                                x.lastIndex = i;
                                m = x.exec(s);

                                if (m === null) {

                                        return s ;

                                } else {

                                        v = m.shift();

                                        if (v.length === 1) {

                                                return v ;

                                        } else {

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

                                        }

                                }

                        } else {

                                return v;

                        }

                }

                return null ;

        }

        function charAtD(i, s) {

                var v, a = A, x = X, m, c;

                if (typeof i === "number" && typeof s === "string") {

                        v = s.charAt(i);

                        if (v === a) {

                                x.lastIndex = i;
                                m = x.exec(s);

                                if (m === null) {

                                        return s ;

                                } else {

                                        v = m.shift();

                                        if (v.length === 1) {

                                                return v ;

                                        } else {

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

                                        }

                                }

                        } else {

                                return v;

                        }

                }

                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(20, "something something & something something");
s = charAtA(20, "something something &amp; something something");
s = charAtA(20, "something something &#38; something something");

s = charAtA(18, "something something & something something");
s = charAtA(18, "something something &amp; something something");
s = charAtA(18, "something something &#38; something something");

s = charAtA(22, "something something & something something");
s = charAtA(22, "something something &amp; something something");
s = charAtA(22, "something something &#38; something something");
pending…
charAt B
s = charAtB(20, "something something & something something");
s = charAtB(20, "something something &amp; something something");
s = charAtB(20, "something something &#38; something something");

s = charAtB(18, "something something & something something");
s = charAtB(18, "something something &amp; something something");
s = charAtB(18, "something something &#38; something something");

s = charAtB(22, "something something & something something");
s = charAtB(22, "something something &amp; something something");
s = charAtB(22, "something something &#38; something something");
pending…
charAt C
s = charAtC(20, "something something & something something");
s = charAtC(20, "something something &amp; something something");
s = charAtC(20, "something something &#38; something something");

s = charAtC(18, "something something & something something");
s = charAtC(18, "something something &amp; something something");
s = charAtC(18, "something something &#38; something something");

s = charAtC(22, "something something & something something");
s = charAtC(22, "something something &amp; something something");
s = charAtC(22, "something something &#38; something something");
pending…
charAt D
s = charAtD(20, "something something & something something");
s = charAtD(20, "something something &amp; something something");
s = charAtD(20, "something something &#38; something something");

s = charAtD(18, "something something & something something");
s = charAtD(18, "something something &amp; something something");
s = charAtD(18, "something something &#38; something something");

s = charAtD(22, "something something & something something");
s = charAtD(22, "something something &amp; something something");
s = charAtD(22, "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