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

Re: Why is querySelector much slower?

From: Glen Huang <curvedmark@gmail.com>
Date: Tue, 28 Apr 2015 13:58:59 +0800
Cc: public-webapps <public-webapps@w3.org>
Message-Id: <B7636824-6BFF-4AC4-B8E0-90183589423A@gmail.com>
To: Boris Zbarsky <bzbarsky@MIT.EDU>
Wow, it's now super clear. Thanks for the detailed explanation.

Just a quick follow up question to quench my curiosity: if I do "list[1]" and no one has ever asked the list for any element, Gecko will find the first two matching elements, and store them in the list, if I then immediately do "list[0]", the first element is returned without walking the DOM (assuming there are at least two matching elements)? 

> querySelector("foo") and getElementsByTagName("foo")[0] can return different nodes

Still a bit confused regarding this. If the premise is the selector only contains characters allowed in a tag name, how can they return different nodes, maybe I missed something? Unless you mean querySelector(":foo") and getElementsByTagName(":foo")[0] can return different results, which is obvious.

If by parsing the passed selector (or lookup the cached parsed selectors) you know it only contains a tag name, why it is a bit harder to optimize? You just look up the (tagname, root) hash table, no?

> In practice this hasn't come up as a bottleneck in anything we've profiled so far

I'm probably prematurely optimizing my code. But nevertheless learned something quite valuable by asking. Thanks for answering it. :)
Received on Tuesday, 28 April 2015 05:59:32 UTC

This archive was generated by hypermail 2.3.1 : Friday, 27 October 2017 07:27:31 UTC