14

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
g t
  • 7,287
  • 7
  • 50
  • 85
  • 7
    If it's any consolation - it looks pretty. – Metalshark Jul 18 '10 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 Jul 18 '10 at 16:45
  • +1 taking the client's rendering performance into consideration is a great idea. – Pekka Jul 18 '10 at 16:46
  • Google actually does something like this with their instant search. I was on a slow computer one time, and after a couple second, instant search was automatically disabled with a message explaining that the computer could not handle it. – JasCav May 12 '11 at 17:40

3 Answers3

7

This may indirectly solve your problem. Pick a few algorithms and performance tests from this site http://dromaeo.com/ that seem similar to your jQuery plugin. Don't run comprehensive 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 complete 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 computers and older browsers, then it just may make sense to remove the thing altogether.

Anurag
  • 140,337
  • 36
  • 221
  • 257
6

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 compromise 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John Patrick
  • 186
  • 1
  • 2
  • Still, the second idea will not be able to check the computer performance... Indeed, many owners of fast computers are using slow software just because they can't notice that it is slow. – mbq Jul 18 '10 at 16:52
  • Good idea. I don't have any data, but I'm willing to bet that users running IE6 are more likely to have less powerful computers than those running Chrome (for example) – Blair McMillan Jul 18 '10 at 16:53
  • In general yes, I'd think so. And in addition to that, browsers that have slow javascript engines aren't going to perform well for intensive tasks on any machine, so while it's not a 100% solution, disabling intensive, non-critical features based on browser version does help. – John Patrick Jul 18 '10 at 18:27
1

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 computer.

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 computers with > 1024px. But in general a 1024px + computer 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!

Haroldo
  • 36,607
  • 46
  • 127
  • 169