数组累加兼eval性能测试

<label>
    <input type="checkbox" />测试结果
</label>
<script>
    document.getElementsByTagName("input")[0].onclick = function(){
        if(this.checked == true){
            var t1 = new Date().getTime();
            //方法二 +=
            for (i = 0; i < arr.length; i++) {
                tmp += arr[i]
            }
            console.log(tmp);
            var t2 = new Date().getTime();
            console.log(t2 - t1)
            //34ms
        };
    }
    var arr = [],
        tmp = 0;
    for (var i = 0; i < 1000000; i++) {
        arr.push(i * 12)
    }
    var t1 = new Date().getTime();
    //方法一 eval
    console.log(eval(arr.join("+")));
    var t2 = new Date().getTime();
    console.log(t2 - t1)
    //475ms
</script>

  

posted @ 2015-06-19 18:13  Joerias  阅读(206)  评论(0编辑  收藏  举报