Slide 1

Slide 1 text

How JavaScript Works The Journey of JavaScript to Machine code 2020/05/25 Yamato Murakami

Slide 2

Slide 2 text

Agenda - Motivation - Technical terms - How JavaScript Works - Conclusion

Slide 3

Slide 3 text

Motivation

Slide 4

Slide 4 text

JS perfomance article

Slide 5

Slide 5 text

How JavaScript Works

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

1. Fetching source code • Browser parses HTML and detect script tag. • Fetch JavaScript code from Network, Browser cache or Service worker etc… . • Encoded Code gets loaded from one of these resources via byte stream. • ex) ‘function’ <-> 0066 0075 006e 0063 0074 0069 006f 006e • Decoding loaded Code by byte stream decoder. • Pass over decoded ‘tokens' to parser. • token • function hellow () { } • `function` `hellow` `(` `)` `{` `}`

Slide 10

Slide 10 text

(Byte) Stream (Byte) Stream Decoder ɾI/O abstraction between program and recourses ɹ(network, memory, file etc…) ɾOr data type abstracting stream of programming language (ex: Java) ɾDecoder which can decode encoded data in stream

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

2. Parsing source code • Parser builds AST from tokens. • Not all tokens are transformed to AST immediately. • Tokens needed immediately are transformed to AST (by parser). • Tokens not needed immediately transformed to AST when they are needed (by pre parser). • This system is called `lazy parsing`

Slide 13

Slide 13 text

AST ɾ Abbreviation of Abstract Syntax Tree ɾCodes which tokenized and arranged as tree structure

Slide 14

Slide 14 text

parser(V8) pre parser(V8) ɾAST generator from JavaScript code ɾGenerate AST immediately ɾAST generator from JavaScript code ɾGenerate AST on demand

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

3. Generating byte code • Interpreter generates Byte code from AST.

Slide 17

Slide 17 text

Interpreter(V8) Byte code ɾByte code generator from AST ɾMachine code generator from Byte code ɾInstruction set designed for Virtual Machine CPU ɾV8 interpreter is called `ignition` ɾThe language which VM CPU can handle (like Machine code)

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

4. Generating machine code • Interpreter generates Machine code from Byte code. • Ready for manipulating by CPU !

Slide 20

Slide 20 text

Machine code ɾInstruction set designed for actual CPU ɾ010010101001010100101010100101001010011

Slide 21

Slide 21 text

4. Generating machine code • Interpreter generates Machine code from Byte code. • Ready for manipulating by CPU ! • The End…

Slide 22

Slide 22 text

4. Generating machine code • Interpreter generates Machine code and Type Feedback from Byte code. • Type Feedback • meta data to optimize machine code • Including type info, the number of execution of code • Ready for manipulating by CPU ! • The End…

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

5. Optimization • JIT compiler `TurboFan` generates optimized machine code from byte code and type feedback. • How does TurboFan optimize machine code ? • Skip type check • You know JavaScript is dynamical type programming language. • Example code which can optimize.

Slide 25

Slide 25 text

JIT compiler(V8) ɾAbbreviation of Just In Time ɾJIT compiler generates machine code when the program runs. ɾV8 JIT compiler is called `TurboFan`.

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

6. Deoptimization • Turbo fan can not optimize all code. • Example code which can not optimize. • Then Turbo fan restores byte code.

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Conclusion

Slide 30

Slide 30 text

References • https://dev.to/lydiahallie/javascript-visualized-the-javascript-engine-4cdf • http://generator.tubudeco.com/g519/ • https://www.youtube.com/watch?v=voDhHPNMEzg • https://v8.dev/blog/preparser • https://stackoverflow.com/questions/54957946/what-does-v8s-ignition-really-do • https://v8.dev/blog/launching-ignition-and-turbofan • https://blog.hiroppy.me/entry/2017/08/03/095304 • https://docs.google.com/presentation/d/ 1OqjVqRhtwlKeKfvMdX6HaCIu9wpZsrzqpIVIwQSuiXQ/ edit#slide=id.g1357e6d1a4_0_58