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

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