Q: What is Node.js?
A: Node.js refers to JavaScript running on the Web server side. It is based on the Chrome V8 engine and has non-blocking, event-driven I/O features.
Q: Wait, you mentioned Chrome V8, what is it and why do you use it instead of another engine?
A: A JavaScript engine is A program or interpreter that executes JavaScript code. A JavaScript engine can be implemented as A standard interpreter or A just-in-time compiler that compiles JavaScript to bytecode in some form. Its workflow is roughly as follows:
One such engine is the Chrome V8 engine, developed by Google and written in C++, which works almost exactly as shown above:
V8 converts JavaScript code to more efficient machine code (IA-32, x86-64, ARM, or MIPS CPUs) than other JavaScript engines convert to bytecode or interpret execution. It is implemented by a JIT (Just-in-time) compiler without generating bytecode or any intermediate code. Methods such as Inlining, Shapes, and Inline Caches are used to improve performance.
Q: Good, now THAT I know a little bit about what Chrome V8 can do, your list of methods really makes my head spin.
A: For example, Shapes and Inline Caches are used to optimize object property loading.
Q: huh?
A: The ECMAScript specification basically defines all objects as A dictionary of string keys and values mapped to property properties, where the [[]] parentheses are representations of properties that the specification defines that cannot be directly exposed to JavaScript.
Q: In memory?
A: No, no, no. If you store it in memory like this, you’re wasting space. For example, objects with the same shape object = {x: 7, y: 8} have the same attribute name and differ only in [[value]] in the complete dictionary of attribute values.
Q: That should be stored separately, divided by[[value]]
All attribute names other than those are stored separately with the remaining attributes. And it needs to have a property that tells the JavaScript engine where to look for specific values.
JSObject just stores [[value]]. Shape has an Offset that tells JavaScript where to look for the exact value:
When there are multiple objects with the same shape, the advantage becomes clear. Because you only need to store their shape and key-value property information once!
Q: I see, but what does that have to do with Inline Caches?
A: It matters A lot. Shapes are mainly used to implement Inline Caches (ICs), which is one of the key factors for JavaScript to run quickly.
Q:
A: For example, A function that gets an x attribute from an object, when executed in JSC (JavaScriptCore), generates the following bytecode:
Inline Caches, in the first instruction get_by_id, consist of two uninitialized slots.
When the function getX({x: ‘a’}) is called, the object {x: ‘A’} has a Shape containing attribute x, which contains the offset and other properties of attribute x, and stores the Shape and offset in Inline Caches when the function is first executed:
When the function is called later, the Inline Caches only need to compare Shape, and if it is the same as before, only the attribute value needs to be loaded from the offset. That’s a lot faster than every lookup.
Q: Wonderful!
A: V8 does much more than that. In the upcoming 7.2 release, parsing times are significantly reduced, loading times are reduced, and response times are improved:
Q: Well, let’s not talk about V8, I’m interested in the non-blocking and event-driven I/O you mentioned earlier.
A:…
To be continued
reference
- JavaScript engine basics: Shapes and Inline Caches
- V8 version released
- How JavaScript works: inside the V8 engine + 5 tips on how to write optimized code