What is the V8 javascript engine Bytecode?

What is the V8 javascript engine Bytecode?

Have you ever heard of javascript Bytecode?

You might be asking what it is.?


What is a Bytecode

According to Wikipedia, Bytecode (portable code?or?p-code) is an instruction set designed for efficient execution by a software interpreter. Unlike human-readable source code, bytecodes are compact numeric codes, constants, and references (typically numeric addresses) that encode the result of compiler parsing and performing semantic analysis of things like type, scope, and nesting depths of program objects.


To summarize, let's say we are using NodeJS. V8 takes your code and translates it into an optimized instruction set that is not human-readable and will execute the Bytecode.

How NodeJS compiles Javascript into Bytecode

The image below shows you the process it takes to run your excellent javascript code with the v8 engine.

V8 javascript engine
V8 Javascript Engine steps


After NodeJS finish compiling the Bytecode, it will execute it.


How can we export the Bytecode?

You can export a Bytecode from a piece of javascript code like so.

let vmScript = new vm.Script("JavaScript code goes here as text", {produceCachedData: true});

let buffer = vmScript.createCachedData();?        

There is a GitHub project that simplifies this process called Bytenode.

https://github.com/bytenode/bytenode

Follow the instructions to install the package and compile a javascript file into Bytecode. Assuming your file name is named test.js, You can use the CLI like so.?

bytenode --loader -c test.js        

To run the Bytecode, you can use the script below or the generated loader file called test.loader.js.

require('bytenode')

require('./test.jsc');;        

Why not just ship the js file?

Well, take an Electron project. As we are shipping the js bundle to our customers, they can read and make modifications to the project, which is not a good thing for the security of our application.?


Finally, the image below shows this code in the bytecode format in an editor.

console.log("Hello World!, I am Nikan.")        


A javascript byte code for a hello world

要查看或添加评论,请登录

社区洞察

其他会员也浏览了