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