Eval's two sets of performance test data _javascript tips

Source: Internet
Author: User
Tags new set
@ Lao Zhao's one Weibo "is the code generated by eval really inefficient?" Http://t.cn/zWTUBEo contains personal attacks, not happy to enter. ”
Triggered a recent discussion of the eval, the Archbishop @Franky and Gray @otakustay also gave a wonderful data analysis.
Just before I did a similar test, I also follow the crowd to gather a lively, provide two sets of data for your reference.

Update 1: Thanks to the guidance of the gray @otakustay, add a new set of data A3, B3 to eliminate the effect of the eval (') call itself on the results. And to the old all the data re test.
Update 2: Thanks to the tapir of the powerful pat on the steamed brick, increased by 1. A4, B4;A5,B5 test data after eval coverage; 2. A6,b6 eval alias; 3). A7,b7 Eval.call.

Test environment:
A. Machine: Intel (R) corei7-2720 2.2Ghz (4 core 8 threads), Memory 8Gb
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.8112.16421
D. Testing methods
D.1 Each use case is tested 5 times and takes a minimum value.
The Firebug or Chrome Console is not turned on during the D.2 test, and opening these tools multiplies the time and makes it difficult to get the use case results within the time available

Use case A1:
We call the empty eval ("") in the inline function.
Copy Code code 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 the eval ("") in the inline function
Copy Code code 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 A3:
To exclude the effect of the eval ("") call itself, we call eval ("") in the outer function.
Copy Code code 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);
}
}();

Use case A4:
Overwrite the eval () function as a normal null function
Copy Code code 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);
}
}();

Use case A5:

Same function call, not eval, and another null function f
Copy Code code 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 Code code 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);
}
}();

Use case A7:

Use the Eval.call method to invoke the
Copy Code code 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);
}
}();

Group A test results:

A1 A2 A3 A4 A5 A6 A7 A1:a2 A1:a3 A1:a4 A4:a5
Chrome 1612ms 8ms 1244ms 897ms 7ms 718ms 680ms 201.5 1.3 1.8 128.1
Firefox 2468ms 69ms 732ms 2928ms 134ms 5033ms 4984ms 35.8 3.4 0.8 21.9
Ie 1207ms 23ms 233ms 1147ms 37ms 148ms 224ms 52.5 5.2 1.0 31.0
Use case B1:
Copy Code code 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 Code code 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 Code code 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 Code code 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 ("");
}();
}();
}

Use case B5:
Copy Code code 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 Code code 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 Code code 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, "");
}();
}();
}

Group B Test results:
B1 B2 B3 B4 B5 B6 B7 B1:b3 B1:b2 B1:b4 B4:b5
Chrome 1569ms 134ms 1093ms 1022ms 173ms 830ms 916ms 11.7 1.4 1.5 5.9
Firefox 5334ms 1017ms 5503ms 5280ms 1171ms 6797ms 6883ms 5.2 1.0 1.0 4.5
Ie 3933ms 560ms 680ms 4118ms 583ms 745ms 854ms 7.0 5.8 1.0 111.3

Conclusions ( only the case in the text ):

1. The repeated invocation of the eval itself is time-consuming, even with an empty eval ("");

2. Eval has an impact on the efficiency of internal function execution, depending on the environment and Code;

3. We can see that no matter which browsers, either group A or group B, 2 and 5 are better in speed. An eval of an inline function in a descriptive example can still have a significant effect on operational efficiency regardless of how it is invoked (even if the eval is overwritten by a null function). Inference is (black box inference, not authority, probably conjecture) whenever an eval is found in an inline function, even if the eval is a covered empty function, all the externally defined variables are initialized to the current scope in the scope variables. Similarly, Eval will have a significant impact on the optimization function of the JS engine at runtime, reducing the execution efficiency of the inline function.

4. Say Han, although not IE10, but IE9, in the processing of eval, performance is very excellent. IE has been criticized by developers, but its rapid growth is also worth affirming, this example is a good proof.

More detailed reasons for the analysis of the following article description has been very detailed, no longer tired. Welcome to the Brick: especially the greatest ...

@ Lao Zhao's "code generated by eval is really inefficient?" 》
The @Franky of "eval science"
On the influence of Eval in @otakustay

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.