CPU周期,内存使用情况,执行时间等等?

除了感知代码的运行速度之外,是否有一种量化的方法来测试JavaScript的性能?


当前回答

我认为JavaScript性能(时间)测试已经足够了。我在这里找到了一篇关于JavaScript性能测试的非常方便的文章。

其他回答

我认为JavaScript性能(时间)测试已经足够了。我在这里找到了一篇关于JavaScript性能测试的非常方便的文章。

迅速的回答

在jQuery上(更具体地说在Sizzle上),我们使用这个(签出master并在浏览器上打开speed/index.html),它反过来使用benchmark.js。这用于对库进行性能测试。

长回答

如果读者不知道基准测试、工作负载和分析器之间的区别,请先阅读spec.org的“readme 1st”部分中的一些性能测试基础知识。这是针对系统测试的,但是理解这个基础也会有助于JS的性能测试。以下是一些最突出的结果:

What is a benchmark? A benchmark is "a standard of measurement or evaluation" (Webster’s II Dictionary). A computer benchmark is typically a computer program that performs a strictly defined set of operations - a workload - and returns some form of result - a metric - describing how the tested computer performed. Computer benchmark metrics usually measure speed: how fast was the workload completed; or throughput: how many workload units per unit time were completed. Running the same computer benchmark on multiple computers allows a comparison to be made. Should I benchmark my own application? Ideally, the best comparison test for systems would be your own application with your own workload. Unfortunately, it is often impractical to get a wide base of reliable, repeatable and comparable measurements for different systems using your own application with your own workload. Problems might include generation of a good test case, confidentiality concerns, difficulty ensuring comparable conditions, time, money, or other constraints. If not my own application, then what? You may wish to consider using standardized benchmarks as a reference point. Ideally, a standardized benchmark will be portable, and may already have been run on the platforms that you are interested in. However, before you consider the results you need to be sure that you understand the correlation between your application/computing needs and what the benchmark is measuring. Are the benchmarks similar to the kinds of applications you run? Do the workloads have similar characteristics? Based on your answers to these questions, you can begin to see how the benchmark may approximate your reality. Note: A standardized benchmark can serve as reference point. Nevertheless, when you are doing vendor or product selection, SPEC does not claim that any standardized benchmark can replace benchmarking your own actual application.

性能测试JS

理想情况下,最好的性能测试是使用自己的应用程序和自己的工作负载来切换需要测试的内容:不同的库、机器等。

如果这是不可行的(通常是不可行的)。重要的第一步:定义您的工作量。它应该反映应用程序的工作负载。在这次演讲中,Vyacheslav Egorov谈到了你应该避免的糟糕工作量。

然后,你可以使用benchmark.js这样的工具来帮助你收集指标,通常是速度或吞吐量。在Sizzle上,我们感兴趣的是比较修复或更改如何影响库的系统性能。

如果某些东西执行得非常糟糕,那么下一步就是寻找瓶颈。

如何找到瓶颈?分析器

分析javascript执行情况的最佳方法是什么?

这是一个非常老的问题,但我认为我们可以提供一个基于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]`)

的性能。mark (Chrome 87 ^)

performance.mark('initSelect - start');
initSelect();
performance.mark('initSelect - end');

我们总是可以通过简单的date对象来测量任何函数所花费的时间。

var start = +new Date();  // log start timestamp
function1();
var end =  +new Date();  // log end timestamp
var diff = end - start;