程序员的独立思考AND(eval和new Function效率区别)
baidu下一堆博文说new Function比eval要来的高效,一直有个疑惑,都是重新编译一遍js,new Function还要多执行一部函数,为什么new会更高效?
上代码先
<script type="text/javascript"> var str='[{"打算": "aa","反倒是": "dasd","的撒": "dasd","事项": [{"事项": "gdf","事项": "gdf","事项": "df"}]},{"事项": "dsad","事项": "gsg","事项": "fds","事项": [{"名称": "fs","事项": "","事项": ""},{"名称": "","事项": "","事项": ""}]}]'; var i=0; var date=new Date().getTime(); (function(){ //eval(str); (new Function('return '+str))(); i++; if(i!=10000){ arguments.callee(); }else{ var data2=new Date().getTime(); document.write(data2-date); } })() </script>
无论怎么测,无论任何浏览器:
eval都比new Function高效!!!
作为个程序员,不要去人云亦云,自己试过才知道!