0

When a modern browser loads all of the various JavaScript files a page may include (thinking common examples like: jQuery, bootstrap, a developer's custom JS) together into memory, are all of those separate files loaded into a single execution thread? Or does each discrete file get its own thread?

I know JavaScript is single threaded (unless you introduce something like Workers), but I'm confused about whether one thread is used across multiple files or whether each source file gets its own processing thread.

armadadrive
  • 963
  • 2
  • 11
  • 42
  • 3
    All those files eventually make up the single script for the page. The file boundary is arbitrary and doesn't really affect the way the script is executed. – GolezTrol Dec 01 '18 at 17:59
  • So the browser just sort of gloms them all together into one? – armadadrive Dec 01 '18 at 18:00
  • 1
    Yes. When you have multiple scripts loaded, it's the same as concatenating all the scripts into a single file and loading that. – Kresimir Dec 01 '18 at 18:02
  • Ahh, so that's why global scope collisions can happen between files? And how jQuery shares its main object for other scripts to use? – armadadrive Dec 01 '18 at 18:07
  • Somewhere in code it does `window.$ = ....`. – GolezTrol Dec 01 '18 at 23:42
  • @GolezTrol or Kresimir please feel free to add that explanation as an answer, unless you feel this question should be closed as dup? I didn't see it before when searching. – armadadrive Dec 03 '18 at 12:05
  • Looking for a canonical one. I think this one is good, although it doesn't strictly ask about multiple files. The answer do mention though, that there is one thread modifying the DOM, but there can also be workers in the background. So with saying "one thread per DOM" they imply that the various files that are loaded in that script all run in that same thread. https://stackoverflow.com/questions/9726098/is-javascript-single-threaded – GolezTrol Dec 03 '18 at 16:01
  • Yeah, it's the implicit stuff that learners such as myself may not know to infer. I'd seen that question, but the topic of threading in javascript is what gave me this particular question. Anyway, if you'd like to post your comment as an answer I'm happy to accept it. – armadadrive Dec 03 '18 at 16:55

0 Answers0