On Feb 16, 5:30 pm, SteelRing <steelr...@gmail.com> wrote:
> This may sound stupid to y'all jquery practitioners, but i wonder
> which method is fastest (recommended) for selecting a cell with a
> class in a big table (think like 1000+ rows 100+ columns):

Fastest: the browser-native getElementsByClassName  (recent Firefox,
Opera, Safari) is 20 to 40 times faster than $('.className'), as a
bonus you get a live collection.

Alternatively, XPath is probably just as quick but the result isn't
live (supported by the same set of browsers).

The jQuery way is to use a CSS selector, however it is much slower
than other methods (except perhaps in IE).


> ("#tableid tbody tr.rowclass td.cellclass") or is it ("td.cellclass")
> or (".cellclass").
>
> And how about if the cell happens to have a unique id:
>
> ("#tableid tbody tr#uniquerow td#uniqueid") or just ("#uniqueid")

If speed really matters, getElementById is more than 7 times faster
than $('#...') in Firefox and  4 times faster in Safari.


> Philosophically, the question being is it better to put as much detail
> as structurally already known by the programmer into the selector or
> just anything goes and let jquery handle it as long as it works.

There is no selector optimisation that I can see in jQuery other than
using getElementById after discovering '#...' (and the overhead of
discovering that means it is 4 to 6 times slower than gEBI).

As a general rule, keep the selector to a minimum that concisely
describes the element - cells must be inside table, table section and
row elements, including those in a selector doesn't seem to help.  An
ID for the starting selector likely does.

Do some testing for particular circumstances, the most efficient
method will emerge.  Don't forget to include browser-native methods
where available.


--
Rob

Reply via email to