Sibling Index (v15)

Revision 15 of this benchmark created on


Preparation HTML

<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>

Setup

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);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Original
findRow(node);
ready
jQuery
findRow2(node);
ready
Jack's one
findRow3(node);
ready
Experimental and wrong
findRow4(node);
ready
A little bit weird answer.
findRow5(node);
ready
previousElementSibling
findRow6(node);
ready
previousElementSibling with old browser fix
findRow7(node);
ready
compare node to children
findRow8(node);
ready
previousElementSibling - browser fix in separate loop
findRow9(node);
ready
static index from jQuery data cache
findRow10(node);
ready
findRow9 coffeescript output
findRow11(node);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.