W3C home > Mailing lists > Public > whatwg@whatwg.org > October 2013

Re: [whatwg] Proposal: Adding methods like getElementById and getElementsByTagName to DocumentFragments

From: Ryosuke Niwa <rniwa@apple.com>
Date: Fri, 18 Oct 2013 14:25:27 -0700
Message-id: <C02F75A9-A83C-46D4-AC0F-A00A6C38B5BA@apple.com>
To: Ian Hickson <ian@hixie.ch>
Cc: whatwg <whatwg@lists.whatwg.org>, Tim Streater <tim@clothears.org.uk>, Glenn Maynard <glenn@zewt.org>, "Tab Atkins Jr." <jackalmage@gmail.com>, Boris Zbarsky <bzbarsky@mit.edu>
On Oct 18, 2013, at 1:50 PM, Ian Hickson <ian@hixie.ch> wrote:

> On Fri, 18 Oct 2013, Tab Atkins Jr. wrote:
>> On Fri, Oct 18, 2013 at 11:09 AM, James Greene <james.m.greene@gmail.com> wrote:
>>> I would also love to see `getElementById` added to the 
>>> HTMLElement/Element interface. It would be nice to capitalize on that 
>>> potential perf boost in jQuery as well.
>> There's no perf boost available for searching by id on an arbitrary 
>> element.  The reason you may get a better perf for the normal functions 
>> is that documents cache a map from id->element on themselves, so you 
>> just have to do a fast hash-lookup.  Arbitrary elements don't have this 
>> map (it would be way too much memory cost), so it'll fall back to a 
>> standard tree search, exactly as a querySelector would.
> Well, you _could_ just use the element's document's hash lookup, then walk 
> up the tree from the matching node to see if you reach the element, and 
> only if you don't, then fall back on walking the tree. Assuming most such 
> calls are not failing, that would give you pretty good performance (O(N) 
> on the depth of the subtree, more or less).

That's more or less what WebKit does.  The only situation under which WebKit has to traverse the entire subtree is when there are multiple elements with the same id.  In fact, the name getter access on HTMLCollection cannot always be O(n) or worse for compatibility reasons so I would be surprised if any major browser didn't have a similar optimization implemented one way or another.  Having said that, this is all implementation detail/dependent so I wouldn't focus too much on this arguing one way or another.

Regardless, I'm supportive of adding getElementById on Element as long as we don't expose getElementsBy* functions.

- R. Niwa
Received on Friday, 18 October 2013 21:25:53 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 17:00:12 UTC