jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
<div></div>
<div>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
function useDomTraversal(xpath) {
var chunk = "",
element = document.documentElement,
nodeIndex = 1,
i,
j,
char;
xpath = xpath.substr("/html/".length);
for (i = 0; i < xpath.length; i++) {
char = xpath.charAt(i);
if (chunk === "") {
if (!element) {
return null;
}
element = element.firstElementChild;
}
if (char === "/" || i === xpath.length - 1) {
j = 1;
if (i === xpath.length - 1) {
chunk += char;
}
chunk = chunk.toUpperCase();
do {
if (element.nodeName === chunk) {
if (nodeIndex === j) {
break;
}
j++;
}
} while (element = element.nextElementSibling);
nodeIndex = 1;
chunk = "";
} else if (char === "[") {
nodeIndex = "";
} else if (char === "]") {
nodeIndex = parseInt(nodeIndex, 10);
} else if (typeof nodeIndex === "string") {
nodeIndex += char;
} else {
chunk += char;
}
}
return element;
}
function useQuerySelector(xpath) {
var cssSelector = "",
i,
char;
xpath = xpath.substr("/html/".length);
for (i = 0; i < xpath.length; i++) {
char = xpath.charAt(i);
if (char === "/") {
cssSelector += ">";
} else if (char === "[") {
cssSelector += ":nth-of-type(";
} else if (char === "]") {
cssSelector += ")";
} else {
cssSelector += char;
}
}
return document.querySelector(cssSelector);
}
Ready to run.
Test | Ops/sec | |
---|---|---|
query selector |
| ready |
dom traversal |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.