W3C home > Mailing lists > Public > public-webapps@w3.org > April to June 2012

Re: [selectors-api] Consider backporting find() behavior to querySelector()

From: Marat Tanalin | tanalin.com <mtanalin@yandex.ru>
Date: Wed, 20 Jun 2012 19:34:49 +0400
To: Lachlan Hunt <lachlan.hunt@lachy.id.au>
Cc: Charles McCathieNevile <chaals@opera.com>,Tab Atkins Jr. <jackalmage@gmail.com>,Elliott Sprehn <esprehn@gmail.com>,Simon Pieters <simonp@opera.com>,"Kang-Hao (Kenny) Lu" <kennyluck@csail.mit.edu>,public-webapps.w3.org <public-webapps@w3.org>
Message-Id: <675521340206489@web15f.yandex.ru>
20.06.2012, 18:26, "Lachlan Hunt" <lachlan.hunt@lachy.id.au>:
> In particular, is there really value in adding two distinct methods that
> differ only by whether they return 1 element or a collection? šResolving
> this issue first would help with resolving the naming issue.
> It should be noted that JQuery/sizzle does not use querySelector() at
> all, AFAICS. It only uses querySelectorAll() and sometimes switches to
> .getElementById() or document.body.

The whole jQuery's paragigm of always searching all matching elements is a potentially big shortcoming of jQuery as for (at least) performance. It's natural to suppose that searching for just _first_ matching element and returning immediately once it's found should be much _faster_ than searching for _all_ matching elements (be it 100 or 1000 elements) even if we need just first one.

So yes, we need two separate methods: one to select first matching element and another one to select all matching elements. They could be united into one single method with an optional argument, but I doubt it would be quite handy and nonconfusing for using. Also, a method generally should return consistent result as for type of returning value, so if we had one method that always returns collection of elements, that would force us to often use the method with item accessor (like find('A')[0]) that would be redundant, unusable and less readable.

For example, I very like `querySelector()` and use it at least as much as (and maybe even more often than) `querySelectorAll()`.
Received on Wednesday, 20 June 2012 15:35:29 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 18:13:34 UTC