StringEngine charAt
JavaScript performance comparison
Preparation code
<script type="text/javascript">
var FROMHTMLCODE = {
"&": "\u0026"
},
FROMHTMLNAME = {
"&": "\u0026"
},
XA = /\u0026[\w]+\u003b|\u0026\u0023[\d]+\u003b/g,
XB = /\u0026[\w]+\u003b|\u0026\u0023[\d]+\u003b|\u0026+/g,
A = "\u0026", N = 38;
function charAtA(i, s) {
var v, a, x, m, c;
if (typeof i === "number" && typeof s === "string") {
v = s.charAt(i);
a = A;
if (v === a) {
x = XA;
x.lastIndex = i;
m = x.exec(s);
if (m === null) {
return v ;
} else {
v = m.shift();
return (typeof (c = FROMHTMLNAME[v]) === "string") ? c : (typeof (c = FROMHTMLCODE[v]) === "string") ? c : s ;
}
} else {
return v;
}
}
return null ;
}
function charAtB(i, s) {
var v, a, x, m, c;
if (typeof i === "number" && typeof s === "string") {
v = s.charAt(i);
a = A;
if (v === a) {
x = XB;
x.lastIndex = i;
m = x.exec(s);
if (m === null) {
return v ;
} 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 charAtC(i, s) {
var v, a, x, m, c;
if (typeof i === "number" && typeof s === "string") {
v = s.charAt(i);
a = "\u0026";
if (v === a) {
x = /\u0026[\w]+\u003b|\u0026\u0023[\d]+\u003b/g;
x.lastIndex = i;
m = x.exec(s);
if (m === null) {
return v ;
} else {
v = m.shift();
return (typeof (c = FROMHTMLNAME[v]) === "string") ? c : (typeof (c = FROMHTMLCODE[v]) === "string") ? c : s ;
}
} else {
return v;
}
}
return null ;
}
function charCodeAt(i, s) {
var v, n, x, m, c;
if (typeof i === "number" && typeof s === "string") {
v = s.charCodeAt(i);
n = N;
if (v === n) {
x = XA;
x.lastIndex = i;
m = x.exec(s);
if (m === null) {
return n ;
} else {
v = m.shift();
return (typeof (c = FROMHTMLNAME[v]) === "string") ? c.charCodeAt(0) : (typeof (c = FROMHTMLCODE[v]) === "string") ? c.charCodeAt(0) : n ;
}
} else {
return v ;
}
}
return null ;
}
function charAtD(i, s) {
var v, a, x, m, c;
if (typeof i === "number" && typeof s === "string") {
v = s.charAt(i);
a = A;
switch (v) {
case a :
x = XA;
x.lastIndex = i;
m = x.exec(s);
if (m === null) {
return a ;
} else {
v = m.shift();
return (typeof (c = FROMHTMLNAME[v]) === "string") ? c : (typeof (c = FROMHTMLCODE[v]) === "string") ? c : s ;
}
default:
return v ;
}
}
return null ;
}
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
charAt A |
|
pending… |
charAt B |
|
pending… |
charAt C |
|
pending… |
charCodeAt |
|
pending… |
charAt D |
|
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:
- Revision 1: published by Jonathan Perry
- Revision 2: published by Jonathan Perry
- Revision 3: published by Jonathan Perry
- Revision 4: published by Jonathan Perry
- Revision 5: published by Jonathan Perry
- Revision 6: published by Jonathan Perry
- Revision 7: published by Jonathan Perry
- Revision 8: published by Jonathan Perry
- Revision 9: published by Jonathan Perry
0 comments