I worked on a small amount of JavaScript code, which under certain conditions lazily loads several different libraries ( Clicky Web Analytics and Sizzle .
This script is downloaded millions of times a day, so optimizing performance is a serious problem. To date, I have used a couple of flags, such as script_loading and script_loaded , to make sure that I do not load one library more than once ("load", I mean requesting scripts after loading the page by inserting a <script> into the DOM).
My question is: instead of relying on these flags, which have become a bit cumbersome and inconvenient in my code (I think callbacks and all the pitfalls of asynchronous code), this is safe for cross-browser (i.e. back to IE 6) , and also not be detrimental to performance, just to call a simple function to insert a <script> element whenever I get a code branch that needs one of these libraries?
The latter will still guarantee that I will load only one library when I need it, and also simplify and reduce the weight of my code base, but I must be absolutely sure that this will not lead to an additional, unnecessary browser request.
My guess is that adding the <script> element several times will not be harmful, as I believe that browsers should recognize the duplicate URL src and rely on a local cached copy. But you know what happens when we assume ...
I hope that someone is familiar enough with the behavior of various modern (and not very modern, such as IE 6) browsers to be able to talk about what will happen in this case.
In the meantime, I will write a test to try to answer this question first hand. My doubt is that it can be difficult and cumbersome to check with confidence in every browser that my script is expected to support.
Thank you in advance for your help and / or input!