如何比较两个Javascript函数的速度? [英] How do I compare the speed of two Javascript functions?
问题描述
我有一些正在阅读某些XML的Javascript。有一个较旧的函数用于从该数据创建一个JSON对象,我编写了一个新函数,希望能更快地创建该JSON对象。
确定哪个函数执行速度更快的最简单,最好的方法是什么?这是一个相当数量的数据,所以知道它有点重要。
谢谢。
I have some Javascript that is reading in some XML. There is an older function which was used to create a JSON object from that data, and I've written a new function that I hope will create that JSON object faster. What is the easiest and best way to determine which function is performing faster? It's a decent amount of data, so it's somewhat important to know. Thanks.
推荐答案
您可以使用 console.time(ID);
和 console.timeEnd(ID);
(info 此处),并在Chrome开发者工具或Firebug中查看结果,如下所示:
You could use console.time("ID");
and console.timeEnd("ID");
(info here), and look the results in the Chrome Developer Tools or Firebug like so:
console.time("oldFunc");
//oldfunc();
console.timeEnd("oldFunc");
console.time("newfunc");
//newfunc();
console.timeEnd("newfunc");
此外,你可以使用 jsperf
这篇关于如何比较两个Javascript函数的速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!