Which one should I choose for eval and new functions?

Source: Internet
Author: User

Test the Code directly.

Copy codeThe Code is as follows: var aa = "{name: 'cora', item: [{age: 11}, {age: 22}, {age: 23}, {age: 23}]} ";
Var now = new Date (). getTime ();
For (var I = 0; I <100000; I ++ ){
Var a = eval ("(" + aa + ")");

}
Var now1 = new Date (). getTime ();
Document. write ("eval time:" + (now1-now) + "<br/> ");

Var now2 = new Date (). getTime ();
For (var I = 0; I <100000; I ++ ){
Var fn = new Function ("return" + aa );
Fn ();
}
Var now3 = new Date (). getTime ();
Document. write ("new Function time:" + (now3-now2) + "<br/> ");

// The results of the test FF are as follows:
// Eval time: 979
// New Function time: 1372

// The test result of IE8 is as follows:
// Eval time: 913
// New Function time: 1037

// The result of the test is as follows:
// Eval time: 211
// New Function time: 251

// Opera
// Eval time: 384
// New Function time: 1024

The test results are the reference data for testing in different browsers. what's strange is why eval is required for testing in each browser. Do we need to use eval?

First, don't worry. Next, with these questions, I finally started another test with curiosity, at this time, I will make a dynamic number of Han numbers for eval and new functions to execute and then look at the effect.

Copy codeThe Code is as follows: var testEval = function (obj ){
<SPAN style = "COLOR: # ff0000"> return eval ('0, '+ obj + ''); </SPAN>
// Return eval ('+ obj + ')');
};

Var testFun = function (obj ){
Var fn = new Function ("return" + obj );
Fn ();
};

Var now = new Date (). getTime ();
For (var I = 0; I <1000; I ++ ){
Var fn = testEval ("function test () {document. write ('I have a donkey and never ride .........');}");
Fn ();
}
Var now1 = new Date (). getTime ();
Document. write ("<br/> ");
Document. write ("eval time:" + (now1-now) + "<br/> ");

Var now2 = new Date (). getTime ();
For (var I = 0; I <1000; I ++ ){
TestFun ("document. write ('I have a donkey and never ride .........');");
}
Var now3 = new Date (). getTime ();
Document. write ("new Function time:" + (now3-now2) + "<br/> ");

// The results of the test FF are as follows:
// Eval time: 495
// New Function time: 50

// The test result of IE8 is as follows:
// Eval time: 34
// New Function time: 20

// The result of the test is as follows:
// Eval time: 7
// New Function time: 4

// Opera
// Eval time: 7
// New Function time: 18

Test the above results. If building a dynamic execution that causes eval to run on FF is super slow, there is little difference between other browsers. We do not need to investigate it too much here.
Return eval ('0, '+ obj + ''); you may think what this 0 means. adding 0 is mainly compatible with all browsers, an error will be reported for versions earlier than IE9.
However, I really don't know how to analyze the true meaning of 0. I just know that adding this will solve the problem of disgusting IE incompatibility.
The preceding two examples show that the eval conversion for JSON strings is obviously faster, and the new Function is faster for dynamic resolution. Here we have two advantages and disadvantages, in addition, the eval compatibility is not good. If the parsing fails, other JS Scripts may not be executed,
But the latter won't. He will only give up eval and replace it with the new Function in view of this Function which is too troublesome for me. If you have any questions, please correct them.

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.