Nothing can stop us now

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

动态执行javascript代码,eval or Function,哪个性能好?

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>动态执行javascript代码</title>
<script type="text/javascript">
    String.prototype.code = function(){
        return (new Function('with(this) { return ' + this + '}' )).call({});
    };

    var sCode = 'Math.round(3+2.5)*1.2';

    var result = 0;
    for(var i=0;i<10000;i++){
        result += sCode.code();
        //result += eval(sCode);
    }
    alert(result);
</script>
</head>
<body>

</body>
</html>

 

 

 

posted on 2013-04-12 22:08  Bruce Tang  阅读(851)  评论(0编辑  收藏  举报