Inside The Javascript Engine: Created Created by Property Tags
Inside The Javascript Engine: Created Created by Property Tags
Created
Property
Tags
Introduction
Learn JavaScript engines is to learn about what goes on when you run your code.
That kind of knowledge helps programmers to write better, faster and smarter
code.
JavaScript engines are just programs that convert JavaScript code into lower level
code, so the computer can understand. This engines are embedded in browsers
and web servers Node.js) to allow run-time compilation and execution of code.
The Engine
There are many engines in the market and you can write our one if you want. Each
engine has some kind of pipeline with an interpreter and an optimizer compiler
pipeline. The interpreter generates bytecode and the optimizer generates highly
optimized machine code. In these article I will use the V8 engine as example.
Parse
The engine makes what we call a Lexical Analysis. We give a JavaScript file to the
engine and it first does a lexical analysis. This breaks the code into something
called tokens to identify their meaning. After that, we know what the code is
trying to do.
Interpreter
Bytecode is not as low level as machine code, but it’s code that is able to be
interpreted by the javascript engine in order to run code.
Profiler
The profiler is responsible to check our code. It also call a monitor that monitors
and watches our code as it runs and makes notes on how we could optimize this
code. Ex: How many times it is being run? What types are used? How we can
optimize this code?
So, if the profiler founds some code that can be optimized, it pass this code to the
Just-In-Time JIT compiler, so it can be compiled and runs faster. We will see
these interpreter/compiler pipeline with more details during these text.
Optimizing compiler
The optimizing compiler’s job is to figure out what your input program does, and
create an output program that it knows will do the same thing, just faster.
It doesn't translate the files on the fly. It works ahead of the time to create a
translation of what code we’ve just written and it compiles down to usually a
language that can be understood by our machines.
Deoptimize
Hidden classes work similarly to the fixed object layouts (classes) used in
languages like Java, except they are created at runtime. The magic here is that
The benefit becomes clear when we have multiple objects. No matter how many
objects there are, as long as they have the same hidden class, we only have to
store the information once!
This topic about optimization is very extensive and has a different name for it. We
can find more information about that here.
The main motivation behind hidden classes is the concept of IC. JavaScript
Engines uses ICs to memorize information where to find properties on objects, so
that we don’t need to repeat expensive property look-up on each property access.
That’s significantly faster than looking up the property each time.
Don’t mess with property attributes of array elements, so they can be stored
and operated on efficiently.
Extra readings