JavaScript 性能优化 --By Google V8 Team Manager
原文:https://developers.google.com/v8/?hl=zh-CN
Be Prepared before writing code[9:35]
Understand how V8 optimzes Javascript;
Write code mindfully;
Learn you tools and how they can help you;
Be Prepared - Hidden Classes[10:30]
Hidden Classes Make JavaScript Faster.
(注:Hidden Class 可以理解为VM内部实现对描述抽象类的描述,共享隐藏类才能让VM更高效)
Limit Compile-time type information
It's expensive to reason about JavaScript types at compile time...
- V8 internally creates hidden classes for objects at runtime.
- Objects with the same hidden class can use the same optimzed generated code.
Initialize all object members in constructor functions;
Always Initialize members in the same order;
(If you add members in different orders, you create a different tree of hidden classes.
And at the end, you'll have objects with two different hidden classes that can't use the same optimized code)
Be Prepared - Numbers[15:30]
We use a technique called tagging. So inside of V8 we pass around values of 32-bit numbers and objects.
But we want to be able to use the same 32 bits to represent both. And that way we can have one code path that can handle, in many cases, the objects and integers. So what we do is we use the bottom bit.
And each of the values have s special meaning. If the bit is se, it's an object pointer. If it's clear, it's what we call small integer or smi. And that's a 31-bit signed integer. Now if you have a numeric value that you're passing around, assigning to a member that is bigger -it's a numeric value that's bigger than 31 signed bits - then it doesn't fit in one of these smis. And we have to create what's called a box for it. We box the number. We turn it into a double and we create a new object to put that number inside of it. And what follows from that is the next speed trap to avoid, which is make sure, whenever possible, you use 31-bit signed numbers for performance critical calculations.
Prefer numberic values that can be represented as 31-bit signed integers.
Be prepared - Arrays[17:25]
- Fast Elements: linear storage for compact key sets.
- Dictionary Elements: hash table storage otherwise.
Use contiguous keys starting at 0 for Arrays. (
Don't pre-allocate large Arrays(e.g. > 64K elements) to their maxium size, instead grow as you go.
Don't delete elements in arrays, especially numberic arrays.
Double Array Unboxing
- Array's hidden class tracks elements types
- Arrays contraining only doubles are unboxed
- Unboxing causes hidden class change
Initialize using array literals for small fixed-sized arrays
Preallocate small arrays to correct size before using them
Don't store non-numeric values(objects) in numeric arrays
Be prepared - Full Compiler[26:36]
V8 has tow compilers
- "Full" compiler can generate good code for any Javascript
- Optimizing compiler produces great code for most JavaScript
"Full" compiler Starts Executing Code ASAP
- Quickly generates good but not great JIT code
- Assumes(almost) nothing about types at compilation time
- Uses Inline Caches(or ICs) to refine knowledge about types while program runs
Inline Caches(ICs) handle Types Efficiently
- Type-dependent code for operations
- Validate type assumptions first, then do work
- Change at runtime via backpathcing as more types are discovered
Monomorphic Better Than Polymophic
- Operations are monomophic if the hidden class is always the same
- Otherwise they are polymorphic
function add(x,y) {
return x + y;
}
add(1,2); //+ in add is monomorphic
add("a", "b") //+ in add becomes polymorphic
Prefer monomorphic over polymorphic whenever is possible.
Type Feedback Makes Code Faster
- Types taken from ICs
- Operations speculatively get inlined
- Monomophic functions and constructors can be inlined entirely
- Inlininig enables other optimizations
Logging What Gets Optimized
d8 --trace-opt prime.js
logs names of optimized functions to stdout
Not Everything Can Be Optimized
Some features prevent the optimizing compiler from running(a "bail-out")
Avoid the speed trap
Optimizing compiler "bail-out" on functions with try{} catch{} blocks.
Maximizing Performance With Exceptions
function perf_sensitive() {
//Do performance-sensitive work here
}
try{
perf_sensitive()
} catch(e) {
//handle exceptions here
}
How to Find Bailouts
d8 --trace-bailout prime.js
logs optimizing compiler bailouts
Invalid assumptions lead to deoptimization[37:55]
Deoptimization...
- ...throws away optimized code
- ...resumes execution at the right place in "full" compiler code
- Reoptimization might be triggered again later, but for the short term, execution slows down.
Passing V8 Options to Chrome
"/Applicaitons/Google Chrome.app/Contents/MacOS/Google Chrome" \--js-flags="--trace-opt --trace-deopt --trace-bailout"
Avoid the speed trap
Avoid hidden class changes in functions after they are optimized
Identify and Understand[39:50]
"Identify and Understand" for V8
- Ensure problem is JavaScript
- Reduce to pure JavaScript(no DOM!)
- Collect metrics
- Locate bottleneck(s)
Prime Generator -- Profile It
%out/ia32.release/d8 prime.js --prof 287107
using teh built-in sampling profiler
- Takes sampe every millisecond
- Writes v8.log
What to expect from the primes Code
function Primes() {
...
this.addPrime = function(i) {
this.primes[this.prime_count++] = i;
}
this.isPrimeDivisible = function(candidate) {
for(var i = 1; i <= this.prime_count; ++i) {
if(candidate % this.primes[i]) == 0) {
return true;
}
}
return false;
}
};
function main() {
p = new Primes();
var c = 1;
while (p.getPrimeCount() < 25000) {
if(!p.isPrimeDivisible(c)) {
p.addPrime(c);
}
c++;
}
print(p.getPrime(p.getPrimeCount()-1));
}
Prediction: Most Time Spent in main
- All properties and functions monomorphic
- All numeric operations are SMIs
- All functions can be inlined
- No deoptimizations or bailouts
(输出省略 @42:50)
Can you spot the bug?
this.isPrimeDivisible = function(candidate) {
for(var i = 1 ; i <= this.prime_count; ++i) {
if (candidate % this.primes[i] == 0) return true;
}
return false;
}
(Hint: primes is an array of length prime_count)
% out/ia32.release/d8 primes-2.js --prof
287107 (省略)
JavaScript is 60% faster than C++
C++
% g++ primes.cc -o primes
% time ./primes real 0m2.955s
user 0m2.952s
sys 0m.001s
JavaScript
% time d8 primes-.js real 0m1.829s
user 0m1.827s
sys 0m0.010s
JavaScript is 17% slower than optimized C++
Fix What Matters[49:59]
Optimize Your Algorithm
this.isPrimeDivisible = function(candidate) {
for(var i = 1 ; i < this.prime_count; ++i) {
var current_prime = this.primes[i];
if(current_prime*current_prime > candidate){
return false;
}
if (candidate % this.primes[i] == 0) return true;
}
return false;
}
Final Results
(输出省略)
That's more than a 350x Speed-up!
Keep Your Eyes on the Road
- Be prepared
- Identify and Understand the Crux
- Fix What matters
JavaScript 性能优化 --By Google V8 Team Manager的更多相关文章
- JavaScript性能优化篇js优化
JavaScript性能优化篇js优化 随着Ajax越来越普遍,Ajax引用的规模越来越大,Javascript代码的性能越来越显得重要,我想这就是一个很典型的例子,上面那段代码因为会被频繁使用, ...
- JavaScript性能优化
如今主流浏览器都在比拼JavaScript引擎的执行速度,但最终都会达到一个理论极限,即无限接近编译后程序执行速度. 这种情况下决定程序速度的另一个重要因素就是代码本身. 在这里我们会分门别类的介绍J ...
- javascript性能优化-repaint和reflow
repaint(重绘) ,repaint发生更改时,元素的外观被改变,且在没有改变布局的情况下发生,如改变outline,visibility,background color,不会影响到dom结构渲 ...
- 摘:JavaScript性能优化小知识总结
原文地址:http://www.codeceo.com/article/javascript-performance-tips.html JavaScript的性能问题不容小觑,这就需要我们开发人员在 ...
- Javascript 性能优化的一点技巧
把优秀的编程方式当成一种习惯,融入到日常的编程当中.下图是今天想到的一点Javascript 性能优化的技巧,分享一下,抛砖引玉.
- JavaScript性能优化小窍门汇总(含实例)
在众多语言中,JavaScript已经占有重要的一席之地,利用JavaScript我们可以做很多事情 , 应用广泛.在web应用项目中,需要大量JavaScript的代码,将来也会越来越多.但是由于J ...
- JavaScript性能优化小知识总结(转)
JavaScript的性能问题不容小觑,这就需要我们开发人员在编写JavaScript程序时多注意一些细节,本文非常详细的介绍了一下JavaScript性能优化方面的知识点,绝对是干货. 前言 一直在 ...
- JavaScript 性能优化技巧分享
JavaScript 作为当前最为常见的直译式脚本语言,已经广泛应用于 Web 应用开发中.为了提高Web应用的性能,从 JavaScript 的性能优化方向入手,会是一个很好的选择. 本文从加载.上 ...
- javascript性能优化之避免重复工作
javascript最重要也最根本的性能优化标准之一是避免工作,避免工作又包括两点,第一,不做不必要的工作,第二,不做重复的已经完成的工作.第一部分可以通过代码重构完成,第二部分不做重复的工作有时候难 ...
随机推荐
- centos磁盘爆满,查找大文件并清理
今天发现vps敲入crontab -e 居然提示 “Disk quota exceeded” 无法编辑.于是"df -h"查了查发现系统磁盘空间使用100%了.最后定位到是/var ...
- iOS:UIMapView地图视图控件的简单使用
可以通过设置MKMapView的mapViewType设置地图类型 MKMapTypeStandard 普通地图 MKMapTypeSatellite 卫星云图 MKMapTypeHybrid 普通 ...
- c++ 字符串函数用法举例
1. substr() 2. replace() 例子:split() 字符串切割: substr 函数原型: , size_t n = npos ) const; 解释:抽取字符串中从pos(默认为 ...
- Intellij IDEA的Hibernate简单应用
1.创建数据库及其表 create database demo; use demo; CREATE TABLE `user` ( `id` int(10) unsigned NOT NULL ...
- ajax练习四留言板
留言界面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- Sails 自定义 model 方法
Sails 自定义 model 方法 在 Sails 中 model 提供了一些原生的静态方法,如 .create(), .update(), .destroy(), .find(), 等. 在实际业 ...
- django中的filter和get的区别 (MultipleObjectsReturned: get() returned more than one Publisher --)(DoesNotExist: Publisher matching query does not exist.)
上面的例子中`` filter()`` 函数返回一个记录集,这个记录集是一个列表. 相对列表来说,有些时候我们更需要获取单个的对象, `` get()`` 方法就是在此时使用的: >>&g ...
- YTU 2610: A改错题--体检情况分析
2610: A改错题--体检情况分析 时间限制: 1 Sec 内存限制: 128 MB 提交: 233 解决: 161 题目描述 注:本题只需要提交标记为修改部分之间的代码,请按照C++方式提交. ...
- VIM下CS命令
01) :vs 文件目录//打开新的目录02) :cs f s 函数名 //查找那些文件中都用这个函数/变量03) :vert diffsplit A函数 //当前函数与A函数做对比(在对比情况下 ...
- poj -3262 Protecting the Flowers (贪心)
http://poj.org/problem?id=3262 开始一直是理解错题意了!!导致不停wa. 这题是农夫有n头牛在花园里啃花朵,然后农夫要把它们赶回棚子,每次只能赶一头牛,并且给出赶回每头牛 ...