jquery filter vs :contains exact match

Benchmark created by Vivek Kumar Bansal on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<ul id="list">
    <li><a href="#">text 1</a></li>
    <li><a href="#">text 2</a></li>
    <li><a href="#">text 3</a></li>
    <li><a href="#">text 4</a></li>
    <li><a href="#">text 5</a></li>
    <li><a href="#">text 6</a></li>
    <li><a href="#">text 7</a></li>
    <li><a href="#">text 8</a></li>
    <li><a href="#">text 9</a></li>
    <li><a href="#">text 10</a></li>
    <li><a href="#">text 11</a></li>
    <li><a href="#">text 12</a></li>
    <li><a href="#">text 13</a></li>
    <li><a href="#">text 14</a></li>
    <li><a href="#">text 15</a></li>
</ul>
<script>
$.expr[':'].textEquals = $.expr.createPseudo(function(arg) {
    return function( elem ) {
        return $(elem).text().match("^" + arg + "$");
    };
});
</script>

Setup

var $A = $('#list').find('a'),
      filter = "text 1";

Test runner

Ready to run.

Testing in
TestOps/sec
:contains+.filter
var yes = $A.filter(":contains(" + filter + ")").filter(function() {
  return $(this).text() === filter;
});
ready
only .filter
var yes = $A.filter(function() {
  return $(this).text() === filter;
});
ready
custom selector
var yes= $A.filter(":textEquals(" + filter + ")")
ready

Revisions

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

  • Revision 1: published by Vivek Kumar Bansal on