Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

javascript - 如何用回调来衡量javascript代码的执行时间(How to measure execution time of javascript code with callbacks)

I have a piece of javascript code that I am executing using the node.js interpreter.(我有一段javascript代码,我正在使用node.js解释器执行。)

for(var i = 1; i < LIMIT; i++){ db.users.save({id : i, name : "MongoUser [" + i + "]"}, function(err, saved) { if( err || !saved ) console.log("Error"); else console.log("Saved"); }); } I want to know how to measure the time taken by these db insert operations.(我想知道如何测量这些数据库插入操作所花费的时间。) I could compute the difference of Date values after and before this piece of code but that would be incorrect because of the asynchronous nature of the code.(我可以计算这段代码之前和之前的Date值的差异,但由于代码的异步性质,这将是不正确的。)   ask by Stormshadow translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use the Node.js console.time() and console.timeEnd() :(使用Node.js console.time()console.timeEnd() :)

var i; console.time("dbsave"); for(i = 1; i < LIMIT; i++){ db.users.save({id : i, name : "MongoUser [" + i + "]"}, end); } end = function(err, saved) { console.log(( err || !saved )?"Error":"Saved"); if(--i === 1){console.timeEnd("dbsave");} };

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...