jQuery $.each vs :contains

Benchmark created on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<ul>
  <li>item 1</li>
  <li>item 2</li>
  <li>item 3</li>
  <li>item 4</li>
  <li>item 5</li>
</ul>

Test runner

Ready to run.

Testing in
TestOps/sec
$.each
  $('ul li').each(function () {
    if ($(this).text() === "item 3") {
      $(this).addClass('item-3');
    }
  });
 
ready
:contains
$('li:contains("item 3")').addClass('item-3');
 
ready
filter
$('li').filter(function(){
    return $(this).text() === 'item 3'
}).addClass('yourClass');

 
ready

Revisions

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