admin管理员组文章数量:1242888
My website has a jQuery script (from Shadow animation jQuery plugin) which constantly changes the colour of box shadow of various <div>
s on the home page.
The animation is not essential, but it does take up a lot of CPU time on slower machines.
Is it possible to find out if the script will run 'too slowly'? I can then disable it before it impacts performance.
Is this even a good idea? If not, is there an easy way to break up the jQuery animate?
My website has a jQuery script (from Shadow animation jQuery plugin) which constantly changes the colour of box shadow of various <div>
s on the home page.
The animation is not essential, but it does take up a lot of CPU time on slower machines.
Is it possible to find out if the script will run 'too slowly'? I can then disable it before it impacts performance.
Is this even a good idea? If not, is there an easy way to break up the jQuery animate?
Share Improve this question edited May 12, 2011 at 17:16 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Jul 18, 2010 at 16:34 g tg t 7,4638 gold badges55 silver badges87 bronze badges 4- 7 If it's any consolation - it looks pretty. – Metalshark Commented Jul 18, 2010 at 16:38
- 2 I'm not a javascript expert, but my guess is "not easily." (I began thinking about a similar idea recently.) So far, my best suggestion might be to add your own performance monitoring via the Date object. Take a sample every so often and if the difference exceeds some threshold, stop the script from doing the animation. But, we'll see if someone has a more correct suggestion! :) – mjschultz Commented Jul 18, 2010 at 16:45
- +1 taking the client's rendering performance into consideration is a great idea. – Pekka Commented Jul 18, 2010 at 16:46
- Google actually does something like this with their instant search. I was on a slow puter one time, and after a couple second, instant search was automatically disabled with a message explaining that the puter could not handle it. – JasCav Commented May 12, 2011 at 17:40
3 Answers
Reset to default 7This may indirectly solve your problem. Pick a few algorithms and performance tests from this site http://dromaeo./ that seem similar to your jQuery plugin. Don't run prehensive tests as they do on the site. Instead, pick fairly small and fast algorithms, and run them for an unnoticeable period of time.
Use a tiny predefined time span to limit how long these tests are allowed to run. Let's say if that span is 200 ms, and on a fast machine with browser A, you can get 100 iterations, while on some random user's machine, it's only able to plete 5 iterations, then you may want to consider disabling it on the user's machine. Tweak and tweak till you find the optimal numbers.
As a bonus, send all test results back to your server so you have a better idea of where your users lie in the speed spectrum. If a big majority of users are using slower puters and older browsers, then it just may make sense to remove the thing altogether.
You could probably do it by timing a few times round a loop which did some intensive processing on page load, but that's going to slow the page and add even further to CPU load, so it doesn't seem like a great solution.
A promise I've used in the past, though, was to make the decision based on browser version, for example, Internet Explorer 6 users get simpler content whereas newer browsers with better JavaScript performance get the animation. That seemed to work pretty well at a practical level. In practice, browser choice is a big factor in JavaScript performance and you might get a 90% fit with what you want very simply just by taking that into account.
You could do something like $(window).width()
to get the browser width. Using this you could make the assumption that anything < 1024px
wide is likely to be either a netbook, smartphone or old puter.
This wouldnt be nearly as accurate as timing a loop, but much more efficient.
Obviously its this rule's a generalisation and there are will be slow puters with > 1024px. But in general a 1024px + puter would typically be able to handle a fair bit of javascript (untill the owner puts on loads of software, virus scans and browser toolbars!)
hope this is useful!
本文标签: jqueryDisable JavaScript function based on the user39s computer39s performanceStack Overflow
版权声明:本文标题:jquery - Disable JavaScript function based on the user's computer's performance - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740112731a2226513.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论