CPU周期,内存使用情况,执行时间等等?
除了感知代码的运行速度之外,是否有一种量化的方法来测试JavaScript的性能?
CPU周期,内存使用情况,执行时间等等?
除了感知代码的运行速度之外,是否有一种量化的方法来测试JavaScript的性能?
当前回答
有些人建议使用特定的插件和/或浏览器。我不会,因为它们只对一个平台有用;在Firefox上的测试运行不能准确地转换到IE7上。考虑到99.999999%的网站有多个浏览器访问,您需要检查所有流行平台上的性能。
我的建议是将其保留在JS中。创建一个包含所有JS测试的基准测试页面,并计时执行。您甚至可以使用ajax将结果发送给您,以保持它完全自动化。
然后在不同的平台上重复。
其他回答
jsPerf试试。这是一个在线javascript性能测试工具,用于对代码段进行基准测试和比较。我一直在用它。
UX Profiler从用户的角度来解决这个问题。它将所有浏览器事件、网络活动等由用户操作(点击)引起的事件进行分组,并考虑到延迟、超时等所有方面。
下面是一个用于时间性能的可重用类。示例包含在代码中:
/*
Help track time lapse - tells you the time difference between each "check()" and since the "start()"
*/
var TimeCapture = function () {
var start = new Date().getTime();
var last = start;
var now = start;
this.start = function () {
start = new Date().getTime();
};
this.check = function (message) {
now = (new Date().getTime());
console.log(message, 'START:', now - start, 'LAST:', now - last);
last = now;
};
};
//Example:
var time = new TimeCapture();
//begin tracking time
time.start();
//...do stuff
time.check('say something here')//look at your console for output
//..do more stuff
time.check('say something else')//look at your console for output
//..do more stuff
time.check('say something else one more time')//look at your console for output
我们总是可以通过简单的date对象来测量任何函数所花费的时间。
var start = +new Date(); // log start timestamp
function1();
var end = +new Date(); // log end timestamp
var diff = end - start;
这是一个非常老的问题,但我认为我们可以提供一个基于es6的简单解决方案来快速测试你的代码。
这是用于执行时间的基本工作台。我们使用performance.now()来提高精度:
/** * Figure out how long it takes for a method to execute. * * @param {Function} method to test * @param {number} iterations number of executions. * @param {Array} list of set of args to pass in. * @param {T} context the context to call the method in. * @return {number} the time it took, in milliseconds to execute. */ const bench = (method, list, iterations, context) => { let start = 0 const timer = action => { const time = performance.now() switch (action) { case 'start': start = time return 0 case 'stop': const elapsed = time - start start = 0 return elapsed default: return time - start } }; const result = [] timer('start') list = [...list] for (let i = 0; i < iterations; i++) { for (const args of list) { result.push(method.apply(context, args)) } } const elapsed = timer('stop') console.log(`Called method [${method.name}] Mean: ${elapsed / iterations} Exec. time: ${elapsed}`) return elapsed } const fnc = () => {} const isFunction = (f) => f && f instanceof Function const isFunctionFaster = (f) => f && 'function' === typeof f class A {} function basicFnc(){} async function asyncFnc(){} const arrowFnc = ()=> {} const arrowRFnc = ()=> 1 // Not functions const obj = {} const arr = [] const str = 'function' const bol = true const num = 1 const a = new A() const list = [ [isFunction], [basicFnc], [arrowFnc], [arrowRFnc], [asyncFnc], [Array], [Date], [Object], [Number], [String], [Symbol], [A], [obj], [arr], [str], [bol], [num], [a], [null], [undefined], ] const e1 = bench(isFunction, list, 10000) const e2 = bench(isFunctionFaster, list, 10000) const rate = e2/e1 const percent = Math.abs(1 - rate)*100 console.log(`[isFunctionFaster] is ${(percent).toFixed(2)}% ${rate < 1 ? 'faster' : 'slower'} than [isFunction]`)