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

Why is querySelector much slower?

From: Glen Huang <curvedmark@gmail.com>
Date: Mon, 27 Apr 2015 16:57:23 +0800
Message-Id: <E18157BB-DFD2-4185-94B4-61AEEE60EB7A@gmail.com>
To: public-webapps <public-webapps@w3.org>
Intuitively, querySelector('.class') only needs to find the first matching node, whereas getElementsByClassName('.class')[0] needs to find all matching nodes and then return the first. The former should be a lot quicker than the latter. Why that's not the case?

See http://jsperf.com/queryselectorall-vs-getelementsbytagname/119 <http://jsperf.com/queryselectorall-vs-getelementsbytagname/119> for the test

I know querySelectorAll is slow because of the static nature of returned NodeList, but this shouldn't be an issue for querySelector.
Received on Monday, 27 April 2015 08:57:56 UTC

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