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
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div id="whereami">2</div>
</div>
function findRow(node){
var i=1;
while(node.previousSibling){
node = node.previousSibling;
if(node.nodeType === 1){
i++;
}
}
return i;
}
function findRow2(node)
{
return $(node).index();
}
function findRow3(node)
{
var i = 1;
while (node = node.previousSibling) {
if (node.nodeType === 1) { ++i }
}
return i;
}
function findRow4(node)
{
return [].indexOf.call(node.parentNode.childNodes, node)
}
function findRow5(node)
{
var i = 2;
while ((node = node.previousSibling) != null)
i += node.nodeType ^ 3;
return i >> 1;
}
function findRow6(node)
{
var i = 1;
while (node = node.previousElementSibling)
++i;
return i;
}
function findRow7(node)
{
var i = 1,
prev;
while (true)
if (prev = node.previousElementSibling) {
node = prev;
++i;
} else if (node = node.previousSibling) {
if (node.nodeType === 1) {
++i;
}
} else break;
return i;
}
function findRow8(node) {
var children = node.parentNode.children,
i = 0,
len = children.length;
for( ; i < len && children[i] !== node; i++)
; // <-- empty statement
return i === len ? -1 : i;
}
function findRow9(node) {
var i = 1,
prev = node.previousElementSibling;
if (prev) {
do ++i;
while (prev = prev.previousElementSibling);
} else {
while (node = node.previousSibling) {
if (node.nodeType === 1) {
++i;
}
}
}
return i;
}
function findRow10(node) {
return jQuery(node).data('index');
}
function findRow11(node) {
var i, prev;
i = 0;
prev = node.previousElementSibling;
if (prev) {
while (true) {
++i;
if (!(prev = prev.previousElementSibling)) {
break;
}
}
} else {
while (node = node.previousSibling) {
if (node.nodeType === 1) {
++i;
}
}
}
return i;
};
var node = document.getElementById('whereami'); //div node to find
// give jQuery's .data() a head start by pre-populating
var childs = node.parentNode.getElementsByTagName('*');
for (var i=0, len = childs.length;i < len;i++){
jQuery(childs[i]).data('index',i);
}
Ready to run.
Test | Ops/sec | |
---|---|---|
Original |
| ready |
jQuery |
| ready |
Jack's one |
| ready |
Experimental and wrong |
| ready |
A little bit weird answer. |
| ready |
previousElementSibling |
| ready |
previousElementSibling with old browser fix |
| ready |
compare node to children |
| ready |
previousElementSibling - browser fix in separate loop |
| ready |
static index from jQuery data cache |
| ready |
findRow9 coffeescript output |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.