So first they take monolithic code and modularize it. Then they bundle it back into a single piece of code. Then they do this:<p><pre><code> <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>
</code></pre>
What if my JavaScript code looks like this:<p>var x = "hello /* world */";<p>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.
I used to be impressed by hacks such as these, but now they mostly make me sad: in 20 years of computer improvements, we still have to resort to ugly hacks like these?<p>The chip on an iPhone runs at what, 600 MHz, maybe 800 MHz? My Apple II ran at 1 MHz. I was counting bytes then. I hope I'll see a day where I don't count bytes anymore :-)
How is this HTML5 specific? Everything written there applies to older versions too.<p>And the title is wrong - they are not using HTML5 to do anything, it's about how to reduce latency by lazy loading modules in the background.
What surprised me was that parsing javascript took much longer than downloading is, even on a mobile phone connection! Anyone knows why after all these years we still don't have a way to download pre-compiled javascript?
Why even bother using a script tag if you're just going to eval the text later? Just make it a text file so you don't have to worry about stripping or escaping comments.