├── README.md
├── files
├── NodeInteractive_High_Speed_JS.pdf
├── What’s new in Google’s V8 JavaScript engine Version 6.9.md
└── 面向前端开发者的V8性能优化(深色背景).pptx
└── images
└── v8.png
/README.md:
--------------------------------------------------------------------------------
1 |
2 | 
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | ### 目录
13 |
14 | #### 什么是V8
15 |
16 | V8 is Google’s open source high-performance JavaScript engine, written in C++. It is used in Google Chrome, the open source browser from Google, and in Node.js, among others. It implements ECMAScript as specified in ECMA-262, and runs on Windows 7 or later, macOS 10.12+, and Linux systems that use IA-32, ARM, or MIPS processors. V8 can run standalone, or can be embedded into any C++ application.
17 |
18 | #### JavaScript引擎
19 |
20 | JavaScript引擎是一个执行JavaScript代码的程序或解释器。
21 |
22 | 下面是实现了JavaScript引擎的一个热门项目列表:
23 |
24 | - `V8`—开源,由Google开发,用C++编写的
25 | - `Rhino`—由Mozilla基金所管理,开源,完全用Java开发
26 | - `SpiderMonkey`—第一个JavaScript引擎,最早用在Netscape Navigator上,现在用在Firefox上。
27 | - `JavaScriptCore`—开源,以Nitro销售,由苹果公司为Safari开发
28 | - `KJS`—KDE的引擎最初由Harri Porten开发,用于KDE项目的Konqueror浏览器
29 | - `Chakra(JScript9)`—Internet Explorer
30 | - `Chakra(JavaScript)`—Microsoft Edge
31 | - `Nashor`— 开源为OpenJDK的一部分,由Oracle的Java语言和工具组开发
32 | - `JerryScript`— 是用于物联网的轻量级引擎
33 |
34 | #### 比较引擎的速度
35 |
36 | 
37 |
38 | [arewefastyet.com](http://arewefastyet.com):是用来比较流行JavaScript引擎速度的,包括Firefox里面的TraceMonkey+JaegerMonkey,Google Chrome的V8和Webkit的Nitro(jsc),会展示三大JavaScript引擎最新代码版本的性能比较图表,现在看,Firefox的JS引擎已经超过Nitro,距离Chrome还有一定距离。:)
39 |
40 | 1. 首先确认一点,JavaScript一直都是解释执行;
41 | 2. 传统JavaScript引擎是在运行时将JavaScript解释编译成了字节码,V8而是编译成机器码,并引入JIT和内联缓存等特性且优化了GC,所以性能上有质的飞跃;
42 | 3. 决定一个语言是解释型还是编译型,其实是根据程序运行时还是编译时获得运行目标平台的代码来决定的,所以JavaScript一直都是解释型。
43 | 4. 传统引擎解释执行过程大概是这样的:JavaScript->AST->字节码->本地机器码(由解释器执行);V8大概是这样:JavaScript源码 -> 抽象语法树 -> 本地机器码。但是这样的优点是可以极致的快,内存方面就飙升。所以新版的V8引擎打算改善这个问题: V8 5.9 引入Ignition 字节码解释器将默认启动 。
44 |
45 | JavaScript度量方式从最初的微基准测试到后来的静态测试集,直到V8即将采用真实网页。
46 |
47 | 
48 |
49 | 和其他JavaScript引擎类似,V8也通过合成基准测试来度量性能指标。刚开始,引擎开发者使用诸如SunSpider、Kraken等微基准测试框架;随着浏览器市场的发展,基准测试进入了新纪元,诸如Octane、JetStream等更加大型的框架被使用,但是它们仍然属于合成基准测试引擎。
50 |
51 | 微基准和静态测试集有它们的优势:它们非常容易理解,运行方便,能够在任意浏览器中执行,容易进行对比。但它们也有很大的劣势:测试用例非常有限,难以模拟现实中众多复杂的网页;另外,基准测试需要经常变化,以满足不停进化的前端框架和前端技术;最后,基于基准测试分数的优化,对于真实用户或者前端开发者来说不一定有感知。
52 |
53 | 通过WebPageReplay和运行时调用状态来度量真实的网页性能
54 |
55 | 基于上述传统基准测试的缺陷,V8团队通过加载真实网站页面来度量真实性能。最终,他们通过基于Chrome的组件WebPageReplay来录制网页请求,并按照需求进行回放。
56 |
57 | 随后,配合WebPageReplay组件,他们又开发了称为运行时调用状态(Runtime Call Stats)的工具,以记录不同JavaScript代码在执行时实际使用到的V8组件。有了这个工具的帮助,不仅能够让使用真实网站来测试V8变得更加方便,同时能够完美展示为了V8在执行不同JavaScript代码时会表现的不同。
58 |
59 | 目前,V8团队已经使用了将近25个网站进行性能度量,来指导V8的优化。这些网站,是从Alexa前100名中,以使用JavaScript框架(React、Polymer、Angular、Ember等等)、地理位置分布以及开发团队与V8团队有合作等因素最终挑选出来。
60 |
61 | 想要深入了解网页和运行时调用状态的测试集开发详情,请收看 BlinkOn 6演讲:真实世界性能。读者也可以自己执行运行时调用状态工具。
62 |
63 | 和真实网页加载的区别
64 |
65 | 通过运行时调用状态工具,可以直观的观察到真实网站性能度量数据,并能够和传统基准测试进行比较,了解不同JavaScript执行时V8的内部情况。
66 |
67 | 从这些对比数据上,我们会发现性能基准测试工具Octane和实际25个测试网站相比差别很大。从下面的图表可以看出,Octane的颜色区域和其他网站测试结果相差很大。当运行Octane时,V8的瓶颈在于JavaScript代码执行,然而在处理真实网页时,V8的瓶颈却是解析和编译。如此大的差别最终会导致针对V8的优化效果不佳,甚至产生反效果。
68 |
69 | 
70 |
71 | 从这个图表还能发现,相比于Octane,Speedometer和现实数据更加接近。Speedometer是一个WebKit的基准测试库,其中包含使用了React、Angular、Ember等框架编写的测试用例,和实际的25个网站获得的数据比较匹配。
72 |
73 | 最终目的:更快的V8引擎
74 |
75 | 在过去一年中,基于真实网站的测试集和运行时调用状态工具已经帮助V8有了大约10%~20%的性能提升。由于之前的优化主要着关注于Chrome的页面加载优化,2位数的性能提升已经是个不错的成绩了。这些优化同样使得Speedometer性能基准测试的分数提升了20%~30%。
76 |
77 | 这些性能的提升,对于使用现代JavaScript框架(或者类似模式JavaScript代码)的网站会有明显反应。其他一些提升,如JavaScript内置功能Object.create和Function.prototype.bind的优化、围绕着对象工厂模式的优化、V8引擎的内联缓存功能实现、实时解析器优化等,对于普通JavaScript执行性能优化也有帮助。
78 |
79 | V8团队将持续使用真实网站加载性能来指导V8引擎优化。
80 |
81 | #### github v8
82 |
83 | https://github.com/v8
84 |
85 | #### 目前的情况
86 |
87 | - [JavaScript的成本](https://juejin.im/post/5a253d746fb9a0451463e197)
88 | - [V8 JavaScript 引擎:高性能的 ES2015+](https://www.w3ctech.com/topic/2035)
89 |
90 | #### V8结构知识点
91 |
92 | - [找出可能影响性能的代码(模式)](https://github.com/xitu/gold-miner/blob/master/TODO/tracing-patterns-hinder-performance.md)
93 | - [前端程序员应该懂点 V8 知识](https://segmentfault.com/l/1500000008618265/play)
94 | - [面向前端开发者的V8性能优化](https://juejin.im/post/59ba6420f265da065166e27d )
95 | - [新 V8 为 NODE.JS 带来的性能变化](https://mp.weixin.qq.com/s/Kztct2x_pYBU7JN6P50uIw)
96 | - [认识 V8 引擎](https://zhuanlan.zhihu.com/p/27628685)
97 | - [JavaScript 是如何工作的:V8 引擎内部机制及5个诀窍编写优化代码的技巧](http://www.zcfy.cc/article/how-javascript-works-inside-the-v8-engine-5-tips-on-how-to-write-optimized-code-4033.html)
98 |
99 | #### 编译 Google V8 引擎
100 |
101 | 翻墙、C++、GN
102 |
103 | - [编译和使用 Google V8 引擎](http://www.jikexueyuan.com/course/523.html)
104 | - [编译 V8 源码](https://zhuanlan.zhihu.com/p/25120909?refer=v8core)
105 |
106 | #### 视频
107 |
108 | - [High Performance JS in V8 - Peter Marshall, Google](https://www.youtube.com/watch?v=YqOhBezMx1o)
109 | - [前端程序员应该懂点 V8 知识](https://segmentfault.com/l/1500000008618265/play)
110 |
111 | http://www.clipconverter.cc/
112 |
113 | #### 分析JS代码
114 |
115 | 由于NodeJS是搭建在V8之上的,所以NodeJS很多「新增语言特性」和「提高性能」等更新都需要依赖V8的发布日程。
116 | 在NodeJS中,通过process.versions可以查看NodeJS依赖模块的版本号,V8就包含其中。
117 |
118 | d8 --trace-opt-verbose add-of-ints.js
119 | node --trace-opt-verbose add-of-ints.js
120 |
121 | d8 --trace-opt-verbose add-of-mixed.js
122 | node --trace-opt-verbose add-of-mixed.js
123 |
124 | d8 --trace-opt --trace-deopt add-of-mixed-dep.js
125 | node --trace-opt --trace-deopt add-of-mixed-dep.js
126 |
127 | 运行 d8 --help 可以查看所有的 d8 命令行参数。如果使用 node,直接运行 node --help 输出的是 node 的命令行参数,如果想查看 V8 的,需要使用 node --v8-options。执行node --harmony, 就可以进入开启了es6特性的repl。
128 |
129 | node --trace-opt test.js
130 | node --trace-bailout test.js 查找不能被优化的函数,重写
131 | node --trace-deopt test.js 查找不能优化的函数
132 |
133 | 后面章节会介绍 V8 的 GC(命令行参数 --trace-gc)以及最有意思的 --allow-natives-syntax。
134 |
135 |
136 | ### 参考
137 |
138 | #### 书籍
139 |
140 | 《两周自制脚本语言》
141 | 《深入浅出Node.js》
142 | 《webkit技术内幕》
143 | 《JavaScript高级程序设计》
144 | 《你所不知道的JavaScript(上卷)》
145 |
146 | #### 文章
147 |
148 | - [V8 性能优化杀手](https://juejin.im/post/5959edfc5188250d83241399)
149 | - [V8如何度量真实数据性能](http://www.infoq.com/cn/news/2017/01/V8-measure-performance-data)
150 | - [在 Windows 平台编译 Google V8 引擎](http://www.jikexueyuan.com/course/523_3.html?ss=1)
151 | - [前端程序员应该懂点 V8 知识](https://segmentfault.com/l/1500000008618265/play)
152 | - [面向前端开发者的V8性能优化](https://juejin.im/post/59ba6420f265da065166e27d )
153 | - [前端精读周刊](https://github.com/dt-fe/weekly)
154 | - [新 V8 为 NODE.JS 带来的性能变化](https://mp.weixin.qq.com/s/Kztct2x_pYBU7JN6P50uIw)
155 | - [认识 V8 引擎](https://zhuanlan.zhihu.com/p/27628685)
156 | - [JavaScript 语法解析、AST、V8、JIT](https://cheogo.github.io/learn-javascript/201709/runtime.html)
157 | - [V8找出可能影响性能的代码(模式)](https://juejin.im/post/59f95af951882574d1723e70)
158 | - [JavaScript 是如何工作的:V8 引擎内部机制及5个诀窍编写优化代码的技巧](https://juejin.im/entry/5a1552aef265da4310480878)
159 | - [翻译JavaScript的成本](https://juejin.im/post/5a253d746fb9a0451463e197)
160 | - [Node.js 十问 ](https://zhuanlan.zhihu.com/p/29650110)
161 | - [Node.js背后的V8引擎优化技术 ](http://geek.csdn.net/news/detail/52207)
162 | - [JavaScript 开发者所需要知道的 V8(一):V8 In NodeJS](https://segmentfault.com/a/1190000007484357)
163 | - [Node 性能优化](https://segmentfault.com/a/1190000007621011)
164 | - [NodeJS的代码调试和性能调优](http://www.barretlee.com/blog/2015/10/07/debug-nodejs-in-command-line/)
165 | - [Node.js 调试 GC 以及内存暴涨的分析](https://blog.eood.cn/node-js_gc)
166 | - [记一次 Node.js 应用内存暴涨分析](http://taobaofed.org/blog/2016/01/14/nodejs-memory-leak-analyze/)
167 |
168 |
169 |
170 |
--------------------------------------------------------------------------------
/files/NodeInteractive_High_Speed_JS.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bluezhan/v8/59df16fc10ffc0e53ee79850963312bdc0fc15d0/files/NodeInteractive_High_Speed_JS.pdf
--------------------------------------------------------------------------------
/files/What’s new in Google’s V8 JavaScript engine Version 6.9.md:
--------------------------------------------------------------------------------
1 | What’s new in Google’s V8 JavaScript engine Version 6.9
2 |
3 | A new beta of Google’s V8 JavaScript engine is now available, for the Chrome 69 browser.
4 |
5 | V8 is a staple in both the Chrome browser and the Node.js JavaScript runtime. WebAsembly is also supported in Mozilla Firefox, Apple Safari, and Microsoft Edge, though those browsers do not use V8.
6 |
7 | [ Go deeper at InfoWorld: Beyond jQuery: An expert guide to JavaScript frameworks • The complete guide to Node.js frameworks • The 10 essential JavaScript developer tools • The 6 best JavaScript IDEs and 10 best JavaScript editors. | Keep up with hot topics in programming with InfoWorld’s App Dev Report newsletter. ]
8 | Future version: New features in Google V8 Version 6.9 beta
9 | The beta of V8 Version 6.9 arrived on August 7, with the production release due in a few weeks in coordination with the planned arrival of the Chrome 69 stable browser. Memory and performance improvements are the focus of the Version 6.9 beta Google’s JavaScript engine.
10 |
11 | For memory savings, Version 6.9 offers embedded built-ins for x64-based computers. These are functions shared by all isolates and embedded onto the binary itself instead of being copied onto the JavaScript heap, thus existing in memory only once regardless of how many isolates are running. V8’s designers have seen an average 9 percent reduction of the heap size across the top 10,000 websites on x64 computers. Support for other platforms will follow in later releases.
12 |
13 | For performance, V8 Version 6.9 reduces Mark-Compact garbage collection pause times by improving WeakMap processing. Concurrent and incremental marking now can process WeakMaps. Previously, this work was done in the final atomic pause of Mark-Compact garbage collection. The garbage collection now also does more work in parallel to lower pause times.
14 |
15 | For performance, DataView methods have been reimplemented in V8 Torque, sparing a costly call to C++ compared to the previous runtime implementation. Also, calls to DataView methods now are inlined when compiling JavaScript into the TurboFan optimizing compiler. This provides better peak performance for hot code.
16 |
17 | V8 Version 6.9 also includes Liftoff, a baseline compiler for the WebAssembly portable code format. It is enabled by default and intended to reduce startup times for WebAssembly-based apps by generating code as quickly as possible. Code itself quality is a secondary priority for Liftoff, with code eventually to be recompiled by V8’s TurboFan compiler.
18 |
19 | [ JavaScript is the most widely deployed language in the world. Whether you're a beginning, intermediate, or advanced JavaScript developer, you'll master new skills with this nine-part course from PluralSight. ]
20 | Liftoff was developed to address an issue in which the back end of the compilation process for TurboFan consumed a lot of time and memory, reducing performance of the WebAssembly code. Liftoff avoids the time and memory overhead of intermediate representation, generating machine code in a single pass over the bytecode of a WebAssembly function. Liftoff and Turbofan give V8 two compilation tiers, with Liftoff a baseline compiler for fast startup and TurboFan providing optimization for performance.
21 |
22 | Google also plans to further improve startup time, cut memory consumption, and bring benefits of Liftoff to more users. These plans involve ports to ARM processors, for use on mobile devices. Liftoff currently works only on Intel 32- and 64-bit platforms. Other improvements under consideration include:
23 |
24 | Implementing dynamic tier-up for mobile devices, to accommodate lower memory volumes on these devices. Experiments are proceeding with a combination of lazy compilation with Liftoff and dynamic tier-up of hot functions in TurboFan.
25 | Improving Liftoff code generation performance and improving the generated code as well.
26 | Where to download the V8 6.9 beta
27 | You can download the V8 6.9 beta from the Google Git repo.
28 |
29 | Current version: What’s new in V8 Version 6.8
30 | Google V8 Version 6.8, released in July 2018, focuses on performance and memory usage.
31 |
32 | Performance has been boosted by array destructuring improvements. The optimizing compiler had not been generating ideal code for array destructuring, so the builders of V8 blocked escape analysis to eliminate temporary allocation, which made array destructuring with a temporary array as fast as a sequence of assignments.
33 |
34 | A new implementation of Object.assign improves performance, via implementation of a fast path for JavaScript.
35 |
36 | Performance for TypedArrays has been increased in instances when sorting is done using a comparison function.
37 |
38 | Other new features in V8 Version 6.8 include:
39 |
40 | To improve execution speed with the WebAssembly portable code format, developers can use trap-based bounds checking, a memory-management optimization, on Linux x64 platforms.
41 | Memory consumption of SFIs (SharedFunctionInfo) has been reduced, via compression and removal of unnecessary fields.
42 | Also to improve memory capabilities, a dependency on SFIs has been broken in which SFIs were unnecessarily kept alive, which had led to the risk of memory leaks.
43 | Where to download the Google V8 6.8
44 | You can download the V8 6.8 code from the Google Git repo.
45 |
46 | Previous version: What’s new in V8 Version 6.7
47 | Google’s V8 JavaScriptengine is getting enhancements for language features and security with the Version 6.7 branch, which is now in production release.
48 |
49 | The V8 6.7 engine has BigInt support enabled by default. Expected in a future version of ECMAScript, BigInts serve as a numeric primitive in JavaScript to represent integers with arbitrary precision. With BigInt, it is possible to perform integer arithmetic without overflowing. BigInt could serve as the basis of an eventual BigDecimal implementation, useful for representing sums of money with decimal precision.
50 |
51 | Also featured in V8 6.7 are more mitigations for side-channel vulnerabilities, intended to prevent information leaks to untrusted code for JavaScript and WebAssembly.
52 |
53 | Where to download the Google V8 6.7
54 | You can download V8 6.7 from the Google Git repo.
55 |
56 | Previous version: What’s new in V8 Version 6.6
57 | Version 6.6 of Google’s V8 JavaScript engine focuses on JavaScript language features and code-caching capabilities.
58 |
59 | For JavaScript, Function.prototype.toString() returns exact slices of source code text, including whitespace and comments. V8 Version 6.6 also implements String.prototype.trimStart() and String.prototype.trimEnd(). This capability had been available through nonstandard trimLeft() and trimRight() methods, which remain as aliases of the new methods to enable backward compatibility.
60 |
61 | Additionally, line and paragraph separator symbols can be used in string literals, thus matching JSON. Previously, these had been treated as line terminators in string literals and their usage resulted in a SyntaxError exception.
62 |
63 | The Array.prototype.values method gives arrays the same iteration interface as the ECMAScript 2015 Map and Set collections. These can be interacted over by keys, values, or entries by calling the same-named method. This change could be incompatible with existing JavaScript code; developers who find odd or broken behavior on a website can try to disable this feature via chrome://flags/#enable-array-prototype-values.
64 |
65 | In another JavaScript programming improvement, the catch clause of try statements can be used without a parameter, which is useful if there is no need for an exception object in the code handling the exception.
66 |
67 | Aside from JavaScript, a code cache after execution capability in Version 6.6 lets more functions be included in the cache, with functions no longer needing to be compiled on future page loads. Compile and parse times in hot load scenarios—in which Chrome visits a page for a third time and provides code previous cached—are reduced. As a result, loading is faster and smoother.
68 |
69 | Other capabilities featured in V8 Version 6.6 include:
70 |
71 | More mitigations to prevent information leaks to untrusted JavaScript and WebAssembly code.
72 | Compile times have been improved by moving out or deprecation of remaining functionality related to AST numbering. A previous compilation process had required a postparsing stage dubbed AST numbering, where nodes in a syntax tree were numbered so compilers using it would have a common point of reference. But this postprocessing pass had ballooned to include other functionality. A new pipeline introduced last year eliminated the need for the numbering, but the numbering pass had remained until Version 6.6.
73 | Asynchronous and array performance has been improved.
74 | Where to download Google V8 Version 6.7
75 | You can download V8 Version 6.7 from Google’s Git repo.
76 |
77 | Previous version: What’s new in V8 Version 6.5
78 | Released in February 2018, in V8 Version 6.5’s streaming compliation, WebAssembly modules are compiled while module bytes are still being downloaded. When all bytes of a single function have been downloaded, the function is passed to a background thread for compilation. As a result, WebAssembly compilation in Chrome 65 can maintain a 50Mbps download speed on high-end machines, Google says—meaning that if WebAssembly is downloaded at that speed, compilation finishes as soon as the download is done.
79 |
80 | Other improvements planned for the 6.5 branch include:
81 |
82 | An untrusted code mode, developed in response to a specualative side-channel Spectre attack. This mode is suitable for applications processing user-generated, untrusted code and is enabled by default.
83 | A mechanism to detect and prevent a deoptimization loop. This loop occurs when optimized code deoptimizes and there is no way to find out what went wrong. V8 developers also have inlined many JavaScript builtins that had been excluded because of a side effect between the load of a function to call and the call itself.
84 | Where to download Google V8 Version 6.5
85 | You can download the V8 Version 6.5 branch from Google’s Git repo.
86 |
87 |
88 | 注:转载https://www.infoworld.com/article/3252818/web-development/whats-new-in-googles-v8-javascript-engine.html?upd=1538873618909
89 |
90 |
--------------------------------------------------------------------------------
/files/面向前端开发者的V8性能优化(深色背景).pptx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bluezhan/v8/59df16fc10ffc0e53ee79850963312bdc0fc15d0/files/面向前端开发者的V8性能优化(深色背景).pptx
--------------------------------------------------------------------------------
/images/v8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bluezhan/v8/59df16fc10ffc0e53ee79850963312bdc0fc15d0/images/v8.png
--------------------------------------------------------------------------------