在运行时确定浏览器是否太慢而无法优雅地处理复杂的 JavaScript/CSS 的最佳方法是什么?
我正在考虑逐步启用/禁用页面上的 JavaScript(和 CSS)效果 - 取决于浏览器的速度有多快/有多慢。
我特别考虑的是低功耗移动设备和旧台式电脑——不仅仅是 IE6 :-)
有没有此类事情的例子?
衡量这一点的最佳方法是什么 - 考虑到诸如繁忙 CPU 上的暂时减速之类的情况?
注释:
- 我对浏览器/操作系统检测不感兴趣。
- 目前,我对带宽测量不感兴趣——只对浏览器/CPU 性能感兴趣。
- 测量可能有趣的事情:
- 基本 JavaScript
- DOM 操作
- DOM/CSS 渲染
- 我希望以尽可能少地影响页面渲染速度的方式来执行此操作。
顺便说一句:为了不让用户因不一致的行为而感到困惑/恼怒 - 当然,这需要屏幕上的通知来允许用户选择加入/退出整个性能调整过程。
[更新:我错过了一个相关问题:根据用户计算机的性能禁用JavaScript功能。谢谢Android!]
I'm toying with the idea of progressively enabling/disabling JavaScript (and CSS) effects on a page - depending on how fast/slow the browser seems to be.
I'm specifically thinking about low-powered mobile devices and old desktop computers -- not just IE6 :-)
Are there any examples of this sort of thing being done?
What would be the best ways to measure this - accounting for things, like temporary slowdowns on busy CPUs?
Notes:
- I'm not interested in browser/OS detection.
- At the moment, I'm not interested in bandwidth measurements - only browser/cpu performance.
- Things that might be interesting to measure:
- Base JavaScript
- DOM manipulation
- DOM/CSS rendering
- I'd like to do this in a way that affects the page's render-speed as little as possible.
BTW: In order to not confuse/irritate users with inconsistent behavior - this would, of course, require on-screen notifications to allow users to opt in/out of this whole performance-tuning process.
[Update: there's a related question that I missed: Disable JavaScript function based on user's computer's performance. Thanks Andrioid!]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
并不是要在这里扫兴,但在我看来,这目前还不可能以任何有意义的方式实现。
造成这种情况的原因有几个,主要是:
无论您做什么测量,如果它有任何意义,都必须测试浏览器/CPU 的最大潜力,而您无法做到这一点并维护任何类型的性能。合理的用户体验
即使可以,这也将是一个毫无意义的快照,因为您不知道测试运行时除了浏览器之外的其他应用程序对 CPU 造成的负载类型,并且无论用户访问您的网站时是否会继续这种情况。
即使你能做到这一点,每个浏览器都有自己的优点和缺点,这意味着,你必须测试每个 dom 操作函数才能知道浏览器完成它的速度,没有“通用”或“根据我的经验,这是有意义的,即使有,dom 操作命令执行的速度也是基于 dom 中当前内容的上下文,当您操作它时,它会发生变化。
您能做的最好的事情就是
让您的用户决定他们想要什么,并让他们在后悔时可以轻松更改该决定
或者更好
选择为他们提供一些您可以合理确定大部分目标受众能够喜欢的东西.
稍微偏离主题,但遵循以下思路:如果您的用户不是其社交圈中的技术领导者(就像这里的大多数用户一样,但世界上的大多数人不是),请不要给他们太多选择,即。任何并非绝对必要的选择 - 他们不想要它,并且在为时已晚之前他们不了解其决定的技术后果。
Not to be a killjoy here, but this is not a feat that is currently possible in any meaningful way in my opinion.
There are several reasons for this, the main ones being:
Whatever measurement you do, if it is to have any meaning, will have to test the maximum potential of the browser/cpu, which you cannot do and maintain any kind of reasonable user experience
Even if you could, it would be a meaningless snapshot since you have no idea what kind of load the cpu is under from other applications than the browser while your test is running, and weather or not that situation will continue while the user is visiting your website.
Even if you could do that, every browser has their own strengths and weaknesses, which means, you'd have to test every dom manipulation function to know how fast the browser would complete it, there is no "general" or "average" that makes sense here in my experience, and even if there was, the speed with which dom manipulation commands execute, is based on the context of what is currently in the dom, which changes when you manipulate it.
The best you can do is to either
Let your users decide what they want, and enable them to easily change that decision if they regret it
or better yet
Choose to give them something that you can be reasonably sure that the greater part of your target audience will be able to enjoy.
Slightly off topic, but following this train of thought: if your users are not techleaders in their social circles (like most users in here are, but most people in the world are not) don't give them too much choice, ie. any choice that is not absolutely nescessary - they don't want it and they don't understand the technical consequences of their decision before it is too late.
另一种不需要明确基准的不同方法是逐步启用功能。
您可以按优先顺序应用功能,在每一项功能之后,如果已经过了一定时间,则放弃其余的功能。
确保最昂贵的功能排在最后,您将根据浏览器的速度向用户提供适当的功能选择。
A different approach, that does not need explicit benchmark, would be to progressively enable features.
You could apply features in prioritized order, and after each one, drop the rest if a certain amount of time has passed.
Ensuring that the most expensive features come last, you would present the user with a somewhat appropriate selection of features based on how speedy the browser is.
您可以尝试对一些基本操作进行计时 - 看看 Steve Souder 的 Episodes 和 Yahoo 的 boomerang,了解在浏览器端计时的好方法。然而,弄清楚这些指标与可接受的性能水平/有益的用户体验之间的关系将相当复杂。
如果您要提供一个 UI 来让用户选择加入/退出,为什么不让用户选择应用程序中的美观程度与渲染速度呢?
You could try timing some basic operations - have a look at Steve Souder's Episodes and Yahoo's boomerang for good ways of timing stuff browserside. However its going to be rather complicated to work out how the metrics relate to an acceptable level of performance / a rewarding user experience.
If you're going to provide a UI to let users opt in / opt out, why not just let the user choose the level of eye candy in the app vs the rendering speed?
看一下 Google 的一些(受版权保护的!)基准 V8:
http://v8.googlecode.com/svn/data/benchmarks/ v4/regexp.js
http://v8.googlecode.com/svn/data/benchmarks/v4/splay.js
我选择了其中的几个更简单的方法可以让您了解您可以自己创建的类似基准来测试功能集。只要在最慢的系统上将测试的运行时间从开始记录的时间到完成记录的时间保持在 100 毫秒以内(这些 Google 测试远大于此值),您就应该获得所需的信息,而无需有损用户体验。虽然 Google 基准测试关心更快系统之间的粒度,但您却不关心。您需要知道的是哪些系统需要比 XX 毫秒更长的时间才能完成。
您可以测试的内容包括正则表达式操作(与上面类似)、字符串连接、页面滚动、任何导致浏览器重绘或回流的内容等。
Take a look at some of Google's (copyrighted!) benchmarks for V8:
http://v8.googlecode.com/svn/data/benchmarks/v4/regexp.js
http://v8.googlecode.com/svn/data/benchmarks/v4/splay.js
I chose a couple of the simpler ones to give an idea of similar benchmarks you could create yourself to test feature sets. As long as you keep the run-time of your tests between time logged at start to time logged at completion to less than 100 ms on the slowest systems (which these Google tests are vastly greater than) you should get the information you need without being detrimental to user experience. While the Google benchmarks care at a granularity between the faster systems, you don't. All that you need to know is which systems take longer than XX ms to complete.
Things you could test are regular expression operations (similar to the above), string concatenation, page scrolling, anything that causes a browser repaint or reflow, etc.
您可以使用 Web Workers 运行您想要的所有基准测试,然后根据结果,将您对机器性能的检测存储在 cookie 中。
当然,这仅适用于 HTML5 支持的浏览器
You could run all the benchmarks you want using Web Workers, then, according to results, store your detection about the performance of the machine in a cookie.
This is only for HTML5 Supported browsers, of-course
一些想法:
.
的网站可能很有用。
.
Some Ideas:
.
<img/>
s..
我遇到了类似的问题,我就这样解决了,事实上它帮助我做出了一些决定。
渲染页面后,我这样做:
这样做之后,我在具有不同操作系统的多个浏览器上对其进行了测试,并且
i
值给出了以下结果:Windows 10 - 8GB RAM:
MacOS 8GB RAM:
Windows 10 - 4GB RAM(这是旧版)我拥有的计算机)
我以列表的形式加载很多 div,它们根据用户的输入动态加载,这帮助我根据给出的性能限制我创建的元素数量,但是
但 JS 还不是全部!因为即使在虚拟机上运行的 Lubuntu 操作系统也给出了很差的结果,它在不到 2 秒的时间内加载了 20,000 个 div 元素,你可以毫无问题地滚动列表,而我花了超过 12 秒对于 IE 来说,性能很糟糕!
所以这可能是一个好方法,但是说到渲染,那就是另一个故事了,但这绝对可以帮助做出一些决定。
祝大家好运!
I came with a similar question and I solved it this way, in fact it helped me taking some decisions.
After rendering the page I do:
After doing that I tested it on several browser with different OS and the
i
value gave me the following results:Windows 10 - 8GB RAM:
MacOS 8GB RAM:
Windows 10 - 4GB RAM (This is an Old computer I have)
I load a lot of divs in a form of list, the are loaded dinamically accordeing to user's input, this helped me to limit the number of elements I create according to the performance the have given, BUT
But the JS is not all!, because even tough the Lubuntu OS running on a virtual machine gave poor results, it loaded 20,000 div elements in less than 2 seconds and you could scroll through the list with no problem while I took more than 12 seconds for IE and the performance sucked!
So a Good way could be that, but When it comes to rendering, thats another story, but this definitely could help to take some decisions.
Good luck, everyone!