3 ms·
So first they take monolithic code and modularize it. Then they bundle it back into a single piece of code. Then they do this: <script id="lazy"> // Make s
by IgorPartola 17y ago
So first they take monolithic code and modularize it. Then they bundle it back into a single piece of code. Then they do this:
<script id="lazy">
// Make sure you strip out (or replace) comment blocks in your JavaScript first.
/*
JavaScript of lazy module
*/
</script>
<script>
function lazyLoad() {
var lazyElement = document.getElementById('lazy');
var lazyElementBody = lazyElement.innerHTML;
var jsCode = stripOutCommentBlock(lazyElementBody);
eval(jsCode);
}
</script>
What if my JavaScript code looks like this:
var x = "hello /* world */";
I wouldn't do it this way. Instead just bundle all your code into a single file and optimize its caching. No HTML5 needed: use HTTP headers.
- IgorPartola 17y agoI guess this is the key part: > On an iPhone 2.2 device, 200k of JavaScript held within a block comment adds 240ms during page load, whereas 200k of JavaScript that is parsed during page load added 2600 ms. That's more than a 10x reduction in startup latency by eliminating 200k of unneeded JavaScript during page load! Take a look at the code sample below to see how this is done. But once again this is an issue of when to eval the code, not when to load it. Put the script tag at the end of the page, so that it gets eval'ed after most of the page is already built.
- ashu 17y agoThat does not stop it from parsing, so even though the page is rendered, it is not interactive.
- IgorPartola 17y agoI guess that's true. Still, XHR respects HTTP headers. You could request the code and immediately get it out of the browser cache if it is there. I see the problem they are trying to solve, but the solution is so obtuse I can't bring myself to using something like this.