peq42
peq42

Reputation: 402

Is it possible to force Node.js to JIT compile code?

Since Node.js uses the V8 js engine, I imagine it works the same way as v8 does and only optimizes/jit compiles a function or some piece of code when it is CPU intensive or gets called many times over.

Considering I'm making a server and I want to get the best performance possible out of my code, and that memory isn't a problem, would there be a way to "force" all my code to be JIT compiled and optimized for performance from the moment it first runs?

Upvotes: 6

Views: 7617

Answers (1)

jmrk
jmrk

Reputation: 40521

V8 developer here. You get the best possible performance by letting V8 do its thing. (We care a lot about performance, and we build V8 such that the out-of-the-box configuration is what gives you the best possible performance.)

For testing purposes, there is indeed a flag that forces "optimized" compilation of all code on first execution. But "optimized" really needs to be in quotes there, because doing so means significantly lower performance than you would normally get. The reason is not only compile time, but also the fact that for a dynamic language like JavaScript, creating optimized code critically depends on observing type feedback first. You can feed code without type feedback to the optimizing compiler, but it won't be able to do a good job -- it has two choices: either produce generic code that can handle any type (which will be about as fast as non-optimized code), or produce code that makes random guesses about which types it'll encounter (which means there's an almost 100% chance that some guess will be wrong and the code will have to be thrown away on first execution). Either way, the resulting performance is worse than the regular way of doing things.

If you want to be able to optimize code ahead of time, write your server in C++, or Rust, or Go, etc. ;-)

Upvotes: 29

Related Questions