你如何测试JavaScript代码? [英] How do you performance test JavaScript code?

查看:151
本文介绍了你如何测试JavaScript代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

CPU Cycles, Memory Usage, Execution Time, etc.?

已添加:除了感知代码的速度之外,是否有一种在JavaScript中测试性能的定量方法运行?

Added: Is there a quantitative way of testing performance in JavaScript besides just perception of how fast the code runs?

推荐答案

Profilers绝对是获取数字的好方法,但根据我的经验,感知性能对于用户/客户端。例如,我们有一个带有Ext手风琴的项目,该手风琴扩展为显示一些数据,然后是几个嵌套的Ext网格。一切实际上都是渲染速度非常快,没有一次操作花费很长时间,只有很多信息一次全部渲染,所以对用户来说感觉很慢。

Profilers are definitely a good way to get numbers, but in my experience, perceived performance is all that matters to the user/client. For example, we had a project with an Ext accordion that expanded to show some data and then a few nested Ext grids. Everything was actually rendering pretty fast, no single operation took a long time, there was just a lot of information being rendered all at once, so it felt slow to the user.

我们固定了这个,而不是通过切换到更快的组件,或优化某些方法,而是首先渲染数据,然后使用setTimeout渲染网格。因此,首先出现信息,然后网格会在一秒钟后出现。总体而言,这样做需要稍微多一点的处理时间,但对于用户而言,感知性能得到了提升。

We 'fixed' this, not by switching to a faster component, or optimizing some method, but by rendering the data first, then rendering the grids with a setTimeout. So, the information appeared first, then the grids would pop into place a second later. Overall, it took slightly more processing time to do it that way, but to the user, the perceived performance was improved.

目前,Chrome分析器和其他工具普遍可用且易于使用, console.time() console.profile() performance.now() 。 Chrome还为您提供了一个时间线视图,可以显示您的帧速率是什么,用户可能在哪里等等。

These days, the Chrome profiler and other tools are universally available and easy to use, as are console.time(), console.profile(), and performance.now(). Chrome also gives you a timeline view which can show you what is killing your frame rate, where the user might be waiting, etc.

查找所有这些工具的文档真的很棒很简单,你不需要SO答案。 7年后,我仍然会重复我原来答案的建议,并指出你可以让代码慢速运行,用户不会注意到它,并且代码运行速度非常快,他们会抱怨相当快的代码不够快。或者您对服务器API的请求花了220毫秒。或者类似的东西。关键在于,如果你拿出一个分析器并去寻找工作,你会发现它,但它可能不是你的用户需要的工作。

Finding documentation for all these tools is really easy, you don't need an SO answer for that. 7 years later, I'll still repeat the advice of my original answer and point out that you can have slow code run forever where a user won't notice it, and pretty fast code running where they do, and they will complain about the pretty fast code not being fast enough. Or that your request to your server API took 220ms. Or something else like that. The point remains that if you take a profiler out and go looking for work to do, you will find it, but it may not be the work your users need.

这篇关于你如何测试JavaScript代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆