selecting limited elements

Benchmark created by Mike on


Preparation HTML

<ul>
<li>link 1</li>
<li>link 2</li>
<li>link 3</li>
<li>link 4</li>
<li class='active'>link 1</li>
<li>link 6</li>
<li>link 7</li>
<li>link 8</li>
<li>link 9</li>
</ul>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Setup

var active = $("li.active");

Test runner

Ready to run.

Testing in
TestOps/sec
eq(0), eq(1)
var el = active.add(active.prevAll(':eq(0), :eq(1)'))
             .add(active.nextAll(':eq(0), :eq(1)'));
ready
slice
var el = active.prevAll().slice(0, 2)
                     .add(active.nextAll().slice(0, 2));

 
ready
index
var activeIndex = active.index();
var el = active.siblings().addBack()
                     .slice(Math.max(0, activeIndex - 2), activeIndex + 3)
                     .not(active);
ready
lt(x)
var el = active.add(active.nextAll(':lt(2)'))
        .add(active.prevAll(':lt(2)'));
ready

Revisions

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