Author: Li Liyuan
Email: lly219 # gmail.com
Preface
I 'd like to discuss one with you before optimizing JavaScript performance.
Below:Premature Optimization
Donald
Knuth
Once said: "premature optimization is the source of all evil "(Premature optimization is the root of all
Edevil
).
In his paper -- Structured
Programming with go to statements
He wrote, "Programmers spend a lot of time thinking, worried, they do not occupy the key part of the program.
However, these problems have a strong negative impact on debugging and the practice of program maintenance. We should forget the impact of small efficiency, such as 97% of the time: Early Optimization
Turning is the source of all evil. "
That is to say, we should not miss the key 3%. A good programmer will not spend a lot of time on how to improve the performance efficiency that does not take up the key part. What he focuses on is the key part.
But usually after the code is determined.
Simply put: Ignore actions and goals rather than focus on unimportant parts too early. Optimized with static thinking, but transaction development is always dynamic
"Optimization" requires long-term implementation.
Practice accumulation can be obtained. The starting point is good, but it is often easy to do bad things, so it takes a lot of time to do something that should not be done, but what to do and what to do is not done. Enhance external conditions and tools, while ignoring internal
In terms of factors and actions, or, too much expectation for the future, ignore the present.
"Do not, ever, optimize prematurelt !"
Body
1. Avoid function calls
Function methodcall (){
Function Square (n) {return N * n };
VaR I = 10000, sum = 0;
While (I-‐) sum + = square (I );
}
Function inlinedmethod (){
VaR I = 10000, sum = 0;
While (I-‐) sum + = I * I;
}
Note: In IE8, methodcall () will pop up a warning box after 1 s: A script on
This page is causing Internet Explorer to run slowly. If it continues
Run, your computer might become unresponsive.
2. Hug Language
2.1
Function
Literals (){
VaR A = [], O = {};
}
Function classic (){
VaR A = new array, O = new object;
}
2.2
Parseint (12.5 );
~~ (1
* "12.5 ")
3 cycles
3.1
VaR test =
'';
For (VAR I = 0; I <10000; I ++)
Test = test + STR;
VaR
Test = ''', I = 10000;
While (I --) test = test + STR;
3.2
Function normalloop (){
VaR
I = 60, j = 0;
While (I--) J ++;
}
Function unrolledloop (){
VaR
J = 0;
J ++;
J ++;
J ++;
J ++;
J ++;
J ++;
J ++;
J ++;
J ++;
J ++;
J ++;
J ++;
J ++;
J ++;
}
4. Global Cache
Function
Uncached (){
VaR I = 10000;
While (I-‐) window. test = 'test ';
}
Function
Cached (){
VaR W = window, And I = 10000;
While (I--) W. test =
'Test ';
5 Expression Exchange
VaR B = false, n = 99;
Function (){
Return
N * N & B;
}
Function (){
Return B & N * N;
}
6 unnecessary parts
6.1
Function (){
VaR
OBJ = {prop: 'test', STR :''};
With (OBJ ){
VaR I =
10000;
While (I-‐) STR + = prop;
Return STR;
}
}
Function (){
VaR OBJ = {prop: 'test', STR: ''}, I =
10000;
While (I-‐) obj. Str + = obj. Prop;
Return obj. STR;
}
6.2
VaR a = 0;
Function (){
Try {
A + = 1;
} Catch (e ){}
}
Function (){
A + = 1;
}
References:
Extreme
Javascript Performance
Http://c2.com/cgi/wiki? Prematureoptimization
Http://www.watch-life.net/life-thinking/no-premature-optimization.html
Http://pplab.snu.ac.kr/courses/adv_pl05/papers/p261-knuth.pdf
Reprinted, please indicate the author's information and source, THX
:)