Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable JavaScript function based on the user's computer's performance

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?

like image 419
g t Avatar asked Jul 18 '10 16:07

g t


People also ask

What will happen if I disable JavaScript?

With JavaScript disabled, your browser will be able to load web pages accurately, especially older websites. Client browsers can insert malicious code into a site's JavaScript code, which can directly impact your computer or device if you visit that website.

What happens if JavaScript is disabled in a web browser?

Many Internet Web sites contain JavaScript, a scripting programming language that runs on the web browser to make specific features on the web page functional. If JavaScript has been disabled within your browser, the content or the functionality of the web page can be limited or unavailable.

How do you turn off a function in JavaScript?

You can do it using the return keyword. Whenever JavaScript sees the return keyword, it immediately exits the function and any variable (or value) you pass after return will be returned back as a result.

Can we disable JavaScript in browser?

Press Ctrl + Shift + P (Windows, Linux) or Command + Shift + P (macOS) to open the Command Menu. Start typing javascript , select Disable JavaScript, and then press Enter to run the command. JavaScript is now disabled.


2 Answers

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.

like image 90
Anurag Avatar answered Oct 17 '22 14:10

Anurag


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.

like image 24
John Patrick Avatar answered Oct 17 '22 15:10

John Patrick