1

Just trying to improve my selector. I have a lot of rows, but i need to exclude rows that has "table_row valuetemplate" class.

<table>      
    <tr class="table_row"></tr>
    <tr class="table_row"></tr>
    <tr class="table_row"></tr>
    <tr class="table_row valuetemplate"></tr>
</table>

I know this very fast :

$(".table_row")

This one is much slower:

$(".table_row:not(.valuetemplate")

Is this the only way to exclude?

Jeremy
  • 22,188
  • 4
  • 68
  • 81
pyccki
  • 693
  • 8
  • 22

2 Answers2

5

$(".table_row:not(.valuetemplate)") appears to be fastest (tested in Chrome 13 & FF 5, results from Chrome)

http://jsperf.com/not-jquery-selectors

$(".table_row").not('.valuetemplate')

  • 17,977 operations/sec
  • ±0.13%
  • 29% slower

$(".table_row:not(.valuetemplate)")

  • 25,386
  • ±1.89%
  • fastest

$("table").children(":not(.valuetemplate)");

  • 17,894
  • ±0.36%
  • 30% slower
MikeM
  • 27,227
  • 4
  • 64
  • 80
0
$(".table_row").not(".valuetemplate");

I am not sure if it's any faster, but I know as a jQuery best practice that using methods instead of specialized selectors is usually faster. Edit: but not in this case.

For example:

$(".thing .subthing")

is a lot slower than

$(".thing").find(".subthing");
Chad Levy
  • 10,032
  • 7
  • 41
  • 69
  • This does not seem to be true in this case. According to [this jsPerf test](http://jsperf.com/which-not), `:not()` runs faster than `.not()` (as tested on Chrome 13). – George Cummins Aug 16 '11 at 23:47