Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What optimizations do modern JavaScript engines perform?

By now, most mainstream browsers have started integrating optimizing JIT compilers to their JavaScript interpreters/virtual machines. That's good for everyone. Now, I'd be hard-pressed to know exactly which optimizations they do perform and how to best take advantage of them. What are references on optimizations in each of the major JavaScript engines?

Background:

I'm working on a compiler that generates JavaScript from a higher-level & safer language (shameless plug: it's called OPA and it's very cool) and, given the size of applications I'm generating, I'd like my JavaScript code to be as fast and as memory-efficient as possible. I can handle high-level optimizations, but I need to know more about which runtime transformations are performed, so as to know which low-level code will produce best results.

One example, from the top of my mind: the language I'm compiling will soon integrate support for laziness. Do JIT engines behave well with lazy function definitions?

like image 613
Yoric Avatar asked Jan 13 '10 10:01

Yoric


People also ask

What is JavaScript optimization?

Optimization is a special type of JavaScript minification. These kind of minimizers not only delete unuseful white spaces, commas, comments etc. but also help to avoid “dead code”: Google Closure Compiler.

What do JavaScript engines do?

A JavaScript engine is a computer program that executes JavaScript code and converts it into computer understandable language.

How is V8 so fast?

However, V8 does it incrementally, i.e., for each GC stop, V8 tries to mark as many objects as possible. It makes everything faster because there's no need to stop the entire execution until the collection finishes. In large applications, the performance improvement makes a lot of difference.

Does V8 optimize?

That's why it is so essential to implement objects with the same properties in the same order to have the same hidden class. Otherwise, V8 won't be able to optimize your code. In V8 words, you want to stay as much monomorphic as possible.


2 Answers

This article series discusses the optimisations of V8. In summary:

  • It generates native machine code - not bytecode (V8 Design Elements)
  • Precise garbage collection (Wikipedia)
  • Inline caching of called methods (Wikipedia)
  • Storing class transition information so that objects with the same properties are grouped together (V8 Design Elements)

The first two points might not help you very much in this situation. The third might show insight into getting things cached together. The last might help you create objects with same properties so they use the same hidden classes.

This blog post discusses some of the optimisations of SquirrelFish Extreme:

  • Bytecode optimizations
  • Polymorphic inline cache (like V8)
  • Context threaded JIT (introduction of native machine code generation, like V8)
  • Regular expression JIT

TraceMonkey is optimised via tracing. I don't know much about it but it looks like it detects the type of a variable in some "hot code" (code run in loops often) and creates optimised code based on what the type of that variable is. If the type of the variable changes, it must recompile the code - based off of this, I'd say you should stay away from changing the type of a variable within a loop.

like image 93
Brian McKenna Avatar answered Oct 26 '22 20:10

Brian McKenna


I found an additional resource:

  • What does V8 do with that loop?
like image 37
Yoric Avatar answered Oct 26 '22 22:10

Yoric