为什么js可以提高代码效率使用功能?

时间:2015-07-27 13:35:44

标签: javascript performance mongodb

当我在js中使用mongodb时,我发现code1(122ms)和example2(390ms)之间的代码效率不同,example1将代码放在function()中但是example2没有。为什么呢?

例1:

var timePage2 = function(){
    var start = (new Date()).getTime();
    var page1;
    for(var i= 0; i < 10000; i++){        
            page1 = db.test.find({"n":{"$gt":i*100}}).limit(100); 
    }
    var end = (new Date()).getTime();
    var timeDiff = end - start;
     print("Update took2:" + timeDiff+"ms");
}

timePage2()

例2:

 //var timePage2 = function(){
    var start = (new Date()).getTime();
    var page1;
    for(var i= 0; i < 10000; i++){        
            page1 = db.test.find({"n":{"$gt":i*100}}).limit(100); 
    }
    var end = (new Date()).getTime();
    var timeDiff = end - start;
     print("Update took2:" + timeDiff+"ms");
//    }

//     timePage2()

0 个答案:

没有答案