@ Lao Zhao's microblog "is the code generated by eval really inefficient? Http://t.cn/zWTUBEo contains personal attacks, do not like not ."
This has aroused recent discussions on eval, and the instructors @ Franky and @ otakustay have also provided wonderful data analysis.
I have also done similar tests before, and I am also busy with the trend, providing two sets of data for your reference.
Update 1: thanks to the guidance of @ otakustay, add a group of new data A3 and B3 to exclude the impact of eval ('') calls on the results. And retests all the old data.
Update 2: Thanks to @ brief for its powerful brick shoot, added 1 ). eval of A4, B4; A5, B5 overwrite the test data; 2 ). a6, B6 eval alias; 3 ). a7, B7 eval. call.
Test environment:
A. Machine: Intel (R) Corei7-2720 2.2 Ghz (4 Core 8 thread), 8 Gb memory
B. OS: Windows 7 Enterprise SP1 64-bit
C. Browser:
B .1 Google Chrome 21.0.1180.79 m
B .2 Firefox 14.0.1
B .3 ie9.0.811385421
D. Test Method
D.1 each test case is tested five times, and the minimum time consumed is used.
D.2 the Firebug or Chrome Console is not enabled during the test. Enabling these tools will multiply the time and it is difficult to obtain the test results within the effective time.
Use Case A1:
We call an empty eval ("") in the inline function ("")
Copy codeThe Code is as follows:
! Function (){
Var a = 1,
B = 2,
C = true;
Function func (){
Var d = 2;
E =! C;
Eval ("");
}
For (var I = 0; I <2999999; I ++ ){
Func (I, I + 1, I + 2 );
}
}();
Use Case A2:
Comment out eval ("") in the inline function ("")
Copy codeThe Code is as follows:
! Function (){
Var a = 1,
B = 2,
C = true;
Function func (){
Var d = 2;
E =! C;
// Eval ("");
}
For (var I = 0; I <2999999; I ++ ){
Func (I, I + 1, I + 2 );
}
}();
Case A3:
To exclude the impact of eval ("") calls, we call eval ("") in the outer function ("")
Copy codeThe Code is as follows:
! Function (){
Var a = 1,
B = 2,
C = true;
Function func (){
Var d = 2;
E =! C;
}
For (var I = 0; I <2999999; I ++ ){
Eval ("");
Func (I, I + 1, I + 2 );
}
}();
Case A4:
Overwrite the eval () function into a common empty function.
Copy codeThe Code is as follows:
Function eval (){}
! Function (){
Var a = 1,
B = 2,
C = true;
Function func (){
Var d = 2;
E =! C;
Eval ("");
}
For (var I = 0; I <2999999; I ++ ){
Func (I, I + 1, I + 2 );
}
}();
Case A5:
It is also a function call, not eval and another empty function f
Copy codeThe Code is as follows:
Function f (){}
! Function (){
Var a = 1,
B = 2,
C = true;
Function func (){
Var d = 2;
E =! C;
F ("");
}
For (var I = 0; I <2999999; I ++ ){
Func (I, I + 1, I + 2 );
}
}();
Use Case A6:
Assign eval to another variable f and then call f
Copy codeThe Code is as follows:
Var f = eval;
! Function (){
Var a = 1,
B = 2,
C = true;
Function func (){
Var d = 2;
E =! C;
F ("");
}
For (var I = 0; I <2999999; I ++ ){
Func (I, I + 1, I + 2 );
}
}();
Case A7:
Use eval. call to call
Copy codeThe Code is as follows:
! Function (){
Var a = 1,
B = 2,
C = true;
Function func (){
Var d = 2;
E =! C;
Eval. call (null ,'');
}
For (var I = 0; I <2999999; I ++ ){
Func (I, I + 1, I + 2 );
}
}();
Test results of Group:
|
A1 |
A2 |
A3 |
A4 |
A5 |
A6 |
A7 |
A1: A2 |
A1: A3 |
A1: A4 |
A4: A5 |
Chrome |
1612 ms |
8 ms |
1244 ms |
897 ms |
7 ms |
718 ms |
680 ms |
201.5 |
1.3 |
1.8 |
128.1 |
Firefox |
2468 ms |
69 ms |
732 ms |
2928 ms |
134 ms |
5033 ms |
4984 ms |
35.8 |
3.4 |
0.8 |
21.9 |
IE |
1207 ms |
23 ms |
233 ms |
1147 ms |
37 ms |
148 ms |
224 ms |
52.5 |
5.2 |
1.0 |
31.0 |
Use Case B1:
Copy codeThe Code is as follows:
For (var I = 0; I <2999999; I ++ ){
! Function (){
Var a = 1,
B = 2,
C = true;
! Function (){
Var d = 2;
E =! C;
Eval ("");
}();
}();
}
Use Case B2:
Copy codeThe Code is as follows:
For (var I = 0; I <2999999; I ++ ){
! Function (){
Var a = 1,
B = 2,
C = true;
! Function (){
Var d = 2;
E =! C;
// Eval ("");
}();
}();
}
Use Case B3:
Copy codeThe Code is as follows:
For (var I = 0; I <2999999; I ++ ){
! Function (){
Var a = 1,
B = 2,
C = true;
! Function (){
Var d = 2;
E =! C;
}();
}();
Eval ("");
}
Use Case B4:
Copy codeThe Code is as follows:
Var eval = function (){}
For (var I = 0; I <2999999; I ++ ){
! Function (){
Var a = 1,
B = 2,
C = true;
! Function (){
Var d = 2;
E =! C;
Eval ("");
}();
}();
}
Example B5:
Copy codeThe Code is as follows:
Var f = function (){}
For (var I = 0; I <2999999; I ++ ){
! Function (){
Var a = 1,
B = 2,
C = true;
! Function (){
Var d = 2;
E =! C;
F ("");
}();
}();
}
Use Case B6:
Copy codeThe Code is as follows:
Var f = eval;
For (var I = 0; I <2999999; I ++ ){
! Function (){
Var a = 1,
B = 2,
C = true;
! Function (){
Var d = 2;
E =! C;
F ("");
}();
}();
}
Use Case B7:
Copy codeThe Code is as follows:
For (var I = 0; I <2999999; I ++ ){
! Function (){
Var a = 1,
B = 2,
C = true;
! Function (){
Var d = 2;
E =! C;
Eval. call (null ,'');
}();
}();
}
Test results of group B:
|
B1 |
B2 |
B3 |
B4 |
B5 |
B6 |
B7 |
B1: B3 |
B1: B2 |
B1: B4 |
B4: B5 |
Chrome |
1569 ms |
134 ms |
1093 ms |
1022 ms |
173 ms |
830 ms |
916 ms |
11.7 |
1.4 |
1.5 |
5.9 |
Firefox |
5334 ms |
1017 ms |
5503 ms |
5280 ms |
1171 ms |
6797 ms |
6883 ms |
5.2 |
1.0 |
1.0 |
4.5 |
IE |
3933 ms |
560 ms |
680 ms |
4118 ms |
583 ms |
745 ms |
854 ms |
7.0 |
5.8 |
1.0 |
111.3 |
Conclusion (Only the CASE):
1. repeated calls of the eval itself are time-consuming, even if it is an empty eval ("");
2. eval has an impact on the efficiency of inline function execution, depending on the specific environment and code;
3. We can see that no matter which browser, whether it is Group A or Group B, the speed of 2 and 5 is better. In this example, the eval of the inline function will have a great impact on the running efficiency no matter how it is called (even if the eval is overwritten by an empty function. Inference is (Black Box inference, non-authoritative, probably hypothetical) as long as eval is found in the inline function, even if this eval is an overwritable empty function, in Scope Variables, all external Defined Variables and other content will be initialized to the current Scope. Similarly, eval will have a great impact on the JS engine optimization functions of inline functions at runtime, reducing execution efficiency.
4. Although IE10 is not used, IE9 is an out-of-the-box remark. It has excellent performance in eval processing. IE has been criticized by developers, but its rapid growth is also worthy of recognition. This example is a good proof.
More detailed analysis of the causes the following articles are described in great detail and will not be detailed. Welcome to shoot bricks :) especially great...
@ Lao Zhao's "is the code generated by eval really inefficient?"
@ Franky's Eval Science
@ Otakustay: Eval impact