13

What is better to use for performance in JavaScript?

document.children[0].children[1]

vs

document.querySelector('body')

Which is more faster in performance?

user3032469
  • 299
  • 1
  • 2
  • 10
  • 8
    I'd say `document.body`, but it really doesn't matter, you'll never notice the difference – adeneo Jan 12 '16 at 13:33

1 Answers1

33

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)

CoderPi
  • 12,985
  • 4
  • 34
  • 62