Pros and Cons of javascript lazy initialization [closed]
Hi i wanted to hear any pros and cons considering javascript lazy initialization, when script is loaded only when object is used. Any comments would be highly appreciated.
Pros: lower startup time before your page is readable/ready to interact with the user.
Cons: higher latency when the object is used. Complexity. Possible source of bugs where something is needed but the code has not been loaded.
I would say the biggest reason to lazy load a script is if it's not entirely certain that script is going to be used on the page.
But almost every situation where I have considered using lazy loaded scripts, I find that the benefits aren't enough to outweigh the cons. For instance, if you are needing to lazy load a script, chances are the size isn't very large. To load that script will now incur another HTTP request, and if the file had been minified and gzipped in the beginning, it would seem to be much more efficient to bundle it with the other scripts included in the page. This would give the benefit of reducing HTTP requests, minimizing latency of execution, and potentially allowing all of the scripts to be cacheable by the browser.
There are much more benefits to not using lazy loading as well, and in my opinion, lazy loading is just not worth it in the kinds of applications I work with. Of course, your situation might be different.
精彩评论