Nodejs and V8 engine

Source: Internet
Author: User

Motivation

JavaScript is a programming language that has the automatic garbage collection feature.

In the market has such a function of language, generally have the corresponding virtual machine, like Java's JVM, C # 's CLR, PHP Zend.

Virtual machines generally implement code parsing, memory management, layout, garbage collection and other functions.

Unlike those languages that do not have a virtual machine, such as C + +, they require manual memory management.

The compiled files of the C + + language can be run directly.

I think learning a development language, in addition to know some grammatical use, various API calls outside. It is also necessary to learn the corresponding virtual machine. JavaScript, for its special historical reasons, is not the only V8 engine. But for now V8 it is the industry's best JavaScript engine, and it becomes a learning sample.

Today's JavaScript is not just a browser-side, but also because NodeJS's relationship is running on the server. And the browser side is different from the server side of the resource sensitivity is very high. When the scale of business is large, and the amount of concurrency comes up, some very small problems can be magnified. At this time some small memory leaks, will brew disaster.

So as a JavaScript developer, it's important to figure out the console.log(‘hello world‘) intermediate process from typing until the CPU executes.

It is also a guide to how to write high-quality code in a loosely written language of JavaScript.

Want to really do JavaScript full stack, the road long its repair far.

V8 Overview

V8 as a JavaScript engine, originally served in Google Chrome. It has been released as well as open source with Chrome's first release. Now it has a lot of other users in addition to the Chrome browser. such as NodeJS, MongoDB, CouchDB and so on.

JavaScript as Prototype-based Language, based on its use of Prototype inheritance features, V8 use literal translation, that is, the JavaScript code directly compiled into machine code (machines code, some places also called Native Code), and then directly to the hardware execution.

Unlike the traditional "compile-parse-execute" process, V8 handles JavaScript with no binaries or other intermediate codes.

In short, V8 's main job is to "translate JavaScript into machine code and then run"

But this is often a complex process, and it needs to deal with a lot of problems, such as:

    1. Compilation optimizations

    2. Memory management

    3. Garbage collection

I wrote this series of articles, but also from these three large points to start, interpretation of V8 for these content processing.

V8 in Nodejsnodejs Source code overview

NodeJS, how did you introduce V8?

We focus on node's source directory:

. ├──. ├──deps│   ├── ... │   ├──v8│   ├── ... ├── ... ├──lib│   ├── ... │   ├──buffer.js│   ├──child_process.js│   ├──console.js│   ├── ... ├──node--out/release/node├── ... ├──out│   ├── ... │   ├──release|         ├──node|         ├──node.d|         ├──obj|             └──gen|                 ├──...|                 ├──node_natives.h|                 ├── ... │   ├── ... ├──src│   ├── ... │   ├──debug-agent.cc│   ├──debug-agent.h│   ├──env-inl.h│   ├──env.cc│   ├── ... ├── ...

Several directories and files to follow:

/deps/v8: Here is the V8 source folder, you will find that the directory structure is very similar to the V8 source code. Nodejs In addition to transplant V8 source code, but also added some content.

/src: The folder in which the core module is written by C + + is called "builtin module"

/lib: The folder where the core module is written by JavaScript, this part is called "native code", when compiling node source code, will use the tool that comes with V8 js2c.py , convert all the built-in JavaScript to the array inside C + +, generate out/Release/obj/gen/node_natives.h File. Some Native module needs to implement the functionality behind the Builtin module.

/out: This directory is the directory generated after node source code is compiled (command line run make ), which contains the node executable file. When you type on the command line node xxx.js , the file is actually run out/Release/node .

A diagram illustrates the overall process of V8 at node runtime.

When node starts, it has loaded the Native Module,builtin Module into memory. Later JavaScript code needs to be dynamically compiled and parsed by V8.

Nodejs and V8 engine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.