This article will show you which eval and newFunction should you choose? For more information, see
Test the Code directly.
The 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) +"
");
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) +"
");
// 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.
The Code is as follows:
Var testEval = function (obj ){
Return eval ('0, '+ obj + '');
// 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 ("
");
Document. write ("eval time:" + (now1-now) +"
");
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) +"
");
// 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.