views:

347

answers:

3

I’ve noticed that after making a change to a javascript file, clearing the cache, and reloading; one function in particular runs in about 90ms, the next time I load the page, it runs in 40ms, the next time I run it, it runs in 20ms … then never gets faster.

It kind of looks like IE is compiling my javascript and caching that compiled version somewhere, similar to how SQLServer processes queries.

Is that what is happening?

Does anybody know where I can find a clarification of how browsers process javascript?

+1  A: 

I know you asked about IE8, but here is V8 - Google's engine. Includes videos on how V8 works.

http://code.google.com/p/v8/

Daniel A. White
Great videos thanks.
John MacIntyre
+3  A: 

IE8 is not open-source, so one can only make hypotheses; however, open-source browsers (such as Chromium, Firefox, Webkit) do work roughly as you say, as do many other interpreters in non-browser and not necessarily JS settings (compile new sources when first seen or reloaded, cache or save the compiled version for faster execution in the future), so it seems very reasonable that IE's Javascript approach should be very much the same, as you surmised.

Alex Martelli
+4  A: 

You may want to check out Eric Lippert's comment to Peter Torr's blog post Compiled, interpreted, whatever:

JScript Classic acts like a compiled language in the sense that before any JScript Classic program runs, we fully syntax check the code, generate a full parse tree, and generate a bytecode. We then run the bytecode through a bytecode interpreter. In that sense, JScript is every bit as "compiled" as Java. The difference is that JScript does not allow you to persist or examine our proprietary bytecode. Also, the bytecode is much higher-level than the JVM bytecode -- the JScript Classic bytecode language is little more than a linearization of the parse tree, whereas the JVM bytecode is clearly intended to operate on a low-level stack machine.

The post and the comment are from September 2003, but judging from Ralph Sommerer's On JavaScript performance in IE8 post, they haven't changed much in the underlying JScript engine:

Unless the JavaScript engine used in IE (and elsewhere) employs some sort of compilation to native code, it will always lag behind its competitors with respect to performance. From what I gather in their Channel9 appearance they have made improvements in bytecode execution, but their main targets were JavaScript native objects (Array, String, ...) and JavaScript-DOM-interaction.

Grant Wagner
Thanks for the well thought out and presented answer.
John MacIntyre