No more nonsense, directly on the test code
Copy Code code as follows:
var aa = "{name: ' Cola ', 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 is:" + (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 is:" + (NOW3-NOW2) + "<br/>");
After the test results FF effect is as follows
Eval time is: 979
The new function time is: 1372
After the test results IE8 effect is as follows
Eval time is: 913
The new function time is: 1037
After testing the results of the chrome effect is as follows
Eval time is: 211
The new function time is: 251
After the test results Opera
Eval time is: 384
The new function time is: 1024
The test results for different browser test reference data, it is strange why every browser test is eval to be faster, we are not to adopt him?
Pro, don't rush, then look down, with these questions, curiosity I finally launched another test, this time I do a dynamic number to let the eval and new Function to perform a look at the effect
Copy Code 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 little donkey, never rides ... ');
FN ();
}
var now1 = new Date (). GetTime ();
document.write ("<br/>");
document.write ("eval time is:" + (Now1-now) + "<br/>");
var now2 = new Date (). GetTime ();
for (var i = 0; i < 1000; i++) {
Testfun (' I have a little donkey, I never ride ... '); "(document.write);
}
var now3 = new Date (). GetTime ();
document.write ("New function time is:" + (NOW3-NOW2) + "<br/>");
After the test results FF effect is as follows
Eval time is: 495
The new function time is: 50
After the test results IE8 effect is as follows
Eval time is: 34
The new function time is: 20
After testing the results of the chrome effect is as follows
Eval Time is: 7
The new function time is: 4
After the test results Opera
Eval Time is: 7
The new function time is: 18
Results Test if building a dynamic execution lets eval execute on FF is super slow, other browsers are not very different, we don't have to be too much of a liability here
return eval (' 0, ' + obj + '); Maybe people think this 0 is what it means, plus 0 is mainly compatible with all browsers, if not, IE9 the following version will be the error
but the real 0 meaning how to analyze I don't know, I just know that adding this will solve the problem of disgusting ie incompatible.
go through the top two chestnuts to explain if the eval is significantly faster for the conversion of the JSON string, If it is dynamic number parsing then new function to quickly, here say two advantages and disadvantages, there is not very good eval compatibility, if parsing error, may cause other JS script will not execute,
the latter will not, he will only focus on this function I do not like too much trouble, decisively give up eval with the new Function to replace. If there is not a good understanding of the place, please correct me, welcome to the brick.