开发者

Pros and Cons of javascript lazy initialization [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, v开发者_运维知识库isit the help center for guidance. Closed 11 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜