哪种方法选择身体元素更快

Which method is faster to select body element?

本文关键字:元素 体元 方法 选择      更新时间:2023-09-26

在JavaScript中使用什么更能提高性能?

document.children[0].children[1]

document.querySelector('body')

哪个性能更快?

这里有从快到慢的结果:

  1. document.body(到目前为止)
  2. document.querySelector('body')document.children[0].children[1]document.getElementsByTagName('body')[0](大致相同)
  3. document.querySelectorAll("body")[0]$('body')

源JSBench(自己试试)