javascriptparsingpagespeedpage-load-time

Do duplicately loaded Javascript files get parsed multiple times?


This question comes from a page speed perspective.

If the same exact file gets loaded twice, and I cannot avoid the situation for external reasons:

<script src="/src/file.js"></script>
<script src="/src/file.js"></script>

With browser caching enabled, file serving second time around should be fast.

However, does the file get parsed again?

As far as I'm aware, JS parsing interrupts page painting so if it gets parsed again then this would present an issue.


Solution

  • Yes they do! The execution of the code in the loaded JavaScript files run each time the file is loaded.

    This can lead to intentional or unintentional side-effects - typically global vars get wiped out.

    Worse things can happen though, such as multiple event listeners or bindings being attached to DOM elements.