I have a pretty large single page application that’s about 800K. About 500K is data that’s sent to the client. I’m considering moving some of my elements that are just json strings in the head in the <script> tag.
When the browser traverses the DOM, does it also look in the <head> tag (in which case there would be no benefit) of does traversing occur only inside the <body> tag?
When using
document.querySelectorAll("some selector example"), elements in the head are also considered. This can easily be solved by using:The same applies to
getElementsByTagName,getElementsByClassName,querySelector, etc.But not to
getElementById, since an id must be unique within a document, so limiting the context wouldn’t make sense.Text is also a node type, by the way.
<script> ...500kb... </script>generates the following DOM tree: