Do browsers parse javascript on every page load?
stackoverflow.com
stackoverflow.com
Fetching js over the net is slow (multiple orders of magnitude slower than parsing).
Optimized compilation is slow (an order of magnitude).
But you can't even do baseline compilation without knowing what's in your evironment. For example, JSC (from WebKit) compiles to bytecode. But the compilation process happens lazily, as functions are first executed. (There is a quick pass to check syntax, beforehand. V8 does this too AFAIR.)
But OK, let's say you're parsing. You parse, create an AST, and go to compile it. The result of bytecompiling for V8 is native code! You can't cache that effectively, it will embed too many runtime constants. For JSC you do produce bytecode, but that bytecode too is dependent on runtime object layouts: for example if you reference a binding in the global object, and the binding is present, then the compiler will produced an indexed lookup into the global. That depends on what's in the global when the compilation unit (function, eval, or global code) is compiled. Caching is a security / correctness risk, in that context.
And then you have the negative impacts of caches on browser memory footprint (and associated GC cost).
It's no wonder why modern JS engines prefer to just keep around the source, and re-parse on demand.
[0] A Hidden Cost of Javascript (2010) http://carlos.bueno.org/2010/02/measuring-javascript-parse-a...
I wonder though, how do you know that is parsing time? There are a number of things happening when JS is loaded, and parsing is only one of them.
Also, an update on that article would probably be useful -- a number of things have changed in the major browsers since you wrote it.
Happy hacking :)
"We don't currently cache the bytecode (or the native code). It is an option we have considered, however, currently, code generation is a trivial portion of JS execution time (< 2%), so we're not pursuing this at the moment."
Basically, it's not going to be a bigger saver compared to all the other parts of the process (e.g. fetching the file over the network).
There are like a dozen javascript frameworks that people use...so why not keep the latest copy of jquery and YUI for example on the user's harddrive? Surely there is space for say, 2-3mb of different javascript libraries.
Then all you have to do is simply ignore the jquery/yui inclusion code when you render the page, and just load it from the harddrive instead.
No sweat for you, but your users and developers might actually find that when they use your browser they get a slightly faster experience.
I think linking to the library from Google's CDN is a nice compromise since it greatly increases the chance that it'll already be cached.
You can always link to the framework, so at worst your user will find the site a few seconds slower to load.
Then if there is adoption, you can always be included in the next build of the browser. All these frameworks tend to be tiny(i.e. jquery is huge, but is something like 30 kb), so nothing is stopping the browser from including all the possible frameworks on the harddrive.
One way to do it would be to include an extra attribute on the script tag with a SHA1 hash of the library. For example:
<script type="text/javascript" signature="24bdd1c96c20c3943054c146340243ccf88eef9e" src="http://myserver.com/js/jquery-1.7.js" ></script>
Old browsers would ignore the extra 'signature' attribute, while new browsers could check for that library in their included, pre-optimized list and skip the download if its found.Seems like it would solve a lot of problems at once without affecting backwards compatibility.
In your views you'd have a javascript_include_tag (or just the src tag) to that view's JS file. From there, between clicking on links within the same domain, it'll only reload the parts you tell it to.
There's a gem that does this for Rails https://github.com/nz/pjax-rails and if you're not using Rails there's a version dependent on jQuery https://github.com/defunkt/jquery-pjax.
Hmmmm - is that across sessions? Does that mean I can detect if a user has visited other sites by timing how long it takes to eval a static script copied from those sites?
Does anyone have any good sources on a) caching of CSS parse trees b) what the latest thinking is on CSS file size, # of selectors, complexity of selectors for page load times.
Thinking of writing something about this myself but I am wondering what's out there.
I recently wrote a summary of our (related) analysis and experience with jQuery Mobile and Sencha Touch - http://blog.roveb.com/post/17259708005/our-experience-with-j...
Google found that moving from a 10-result page loading in
0.4 seconds to a 30-result page loading in 0.9 seconds
decreased traffic and ad revenues by 20%. Tests at Amazon
revealed similar results: every 100ms increase in load time
of Amazon.com decreased sales by 1%.