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

RE: Why is querySelector much slower?

From: François REMY <francois.remy.dev@outlook.com>
Date: Mon, 27 Apr 2015 12:27:33 +0200
Message-ID: <DUB130-W57BD8D05EC0FD3102851B8A5E90@phx.gbl>
To: Glen Huang <curvedmark@gmail.com>, public-webapps <public-webapps@w3.org>
Not sure this is a public-webapps issue but I'm pretty sure however that the reason is the return values of << getElement*By*(...) >> are cached by the browser which means that you end up not doing the work at all at some point in the loop, while you probably do it every single time for << querySelector >> which cannot return a cached result.

From: curvedmark@gmail.com
Date: Mon, 27 Apr 2015 16:57:23 +0800
To: public-webapps@w3.org
Subject: Why is querySelector much slower?

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 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 10:28:02 UTC

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