Home  >  Article  >  Web Front-end  >  Which one should I choose between eval and new Function?_Basic knowledge

Which one should I choose between eval and new Function?_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:36:531059browse

Without further ado, let’s go directly to the test code

Copy the code The code is 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) "
");


var now2 = new Date().getTime();
for (var i = 0; i < 100000; i ) {
var fn = new Function("return" aa) ;
                       fn(); />");


//After testing, the FF effect is as follows
// The effect of the test results IE8 is as follows.
// Eval Time is: 913
// New Function Time: 1037
//The test results of Chrome are as follows

//eval time is: 211
//new Function time is: 251

//Test results Opera

//eval time is: 384
//new Function time is: 1024

The test results are reference data for different browser tests. What I find strange is why every browser tests eval faster. Should we use it?


Dear, don’t be impatient. Read on. With these questions, I finally started another test out of curiosity. At this time, I made a dynamic function to let eval and new Function execute respectively. Let’s take a look. See the effect

Copy code

The code is as follows:var testEval = function (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 There is a little donkey that never rides...'); }");
                                                                                                      ;
document.write("
");
document.write("eval time is:" (now1 - now) "
");

var now2 = new Date().getTime();
for (var i = 0; i < 1000; i ) {
var now2 = new Date().getTime(); Don’t ride either...');");
}
var now3 = new Date().getTime();
document.write("new Function time is: " (now3 - now2) "
");


//After testing, the FF effect is as follows
//eval time is: 495
//new Function time is: 50

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

//The test results of Chrome are as follows
//eval time is: 7
//new Function time is: 4

//Test results Opera
//eval time is: 7
//new Function time is: 18

The above results test is super slow to execute EVAL on FF. Other browsers are not much different. We don't have to investigate too much here. ); Maybe you think what this 0 here means. Adding 0 is mainly to be compatible with all browsers. If you don’t add it, versions below IE9 will report an error
But I really don’t know how to analyze the real meaning of 0. I just know that adding this can solve the disgusting IE incompatibility problem
The above two chestnuts show that if it is for the conversion of JSON strings, eval is obviously faster, and if it is dynamic function analysis, then new Function is faster, here is what it says There are two advantages and disadvantages. The other is that eval compatibility is not very good. If there is an error in parsing, other JS scripts may not be executed.
But the latter will not, it will only target this Function me This person doesn't like things that are too troublesome, so he decisively abandons eval and uses new Function instead. If there is anything I don’t understand correctly, please correct me. Comments are welcome.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn