index vs prevall

Benchmark created by gnarf on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<ul>
<li id="first"></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li="test"></li><li></li><li></li><li></li><li id="last"></li>
</ul>
<script>
  var f = $("#first"),
      l = $("#last"),
      sub = jQuery.sub(),
      l2 = sub("#last");
  
  sub.fn.index = function(elem) {
   if (!elem) {
    return this.prevAll().length;
   }
   if (typeof elem === "string") {
    return jQuery.inArray(this[0],
    // If it receives a string, the selector is used
    // If it receives nothing, the siblings are used
    elem ? jQuery(elem) : this.parent().children());
   }
   // Locate the position of the desired element
   return jQuery.inArray(
   // If it receives a jQuery object, the first element is used
   elem.jquery ? elem[0] : elem, this);
  };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
index #first
f.index();
ready
prevall #first
f.prevAll().length
ready
index #last
l.index()
ready
prevall #last
l.prevAll().length
ready
rewritten #last
l2.index();
ready

Revisions

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