Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which method is faster to select body element? [duplicate]

What is better to use for performance in JavaScript?

document.children[0].children[1]

vs

document.querySelector('body')

Which is more faster in performance?

like image 338
user3032469 Avatar asked Jan 12 '16 13:01

user3032469


1 Answers

Here you've the result from fast to slow:

  1. document.body (by far)
  2. document.querySelector('body'), document.children[0].children[1], document.getElementsByTagName('body')[0] (about the same)
  3. document.querySelectorAll("body")[0], $('body')

Source JSBench (try it yourself)

like image 113
CoderPi Avatar answered Oct 05 '22 02:10

CoderPi