Skip to content →

Just in Time (JIT)

JIT came up with learning about the JavaScript V8 Engine. A blog post I read said,

V8 gets its speed from just-in-time (JIT) compilation of JavaScript to native machine code, just before executing it.

source

So what is JIT. It is an acronym for Just in Time. But really, it is a way of determining when to do work.

I love this site’s explanation.

Say you have a function that determines if a number, n, between 2 and 10000 is prime.

A JIT approach would be to run the is_prime function just when it is needed. So if I want to know if 1337 is a prime number, I would call is_prime(1337) and wait for the function to complete.

Another approach, called Ahead of Time, would create a set of prime numbers between 2 – 10000 and store them somewhere in memory. Then, when I call is_prime(1137) it doesn’t have to do the calculation, it only has to do a quick look up of 1337.

One benefit of JIT is that it requires less memory, since it’s not doing any pre-computation and storing values (that may not ever be needed, btw).

One downside of JIT is that it is potentially slower than Ahead of Time, which makes me wonder why the author of that blog post I quoted earlier said V8 gets its speed from JIT compilation…

Published in Today I Learned