Which one should I choose for eval and new functions?

Source: Internet
Author: User

Test the Code directly.

Copy the Code 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 the Code 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.