Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if the page is slow due to 3rd party javascripts?

Tags:

javascript

For those of us who run content websites and deal with Ad networks, combating malicious or malfunctioning rogue ads can be frustrating.

I own a site that embeds a lot of Youtube & Dailymotion videos. Once in awhile, a bad Ad will turn up and make the video playback stutter. I always dealt with these on a case-by-case basis. But, is there a way to detect (using javascript) whether or not the page is slow?

In my head, a very crude way is to have a setInterval running at 100ms. And if it detects a big delay in one interval, act accordingly.

Are there other more elegant approaches?

like image 254
Dave Avatar asked Aug 26 '15 17:08

Dave


People also ask

How can you ensure the loading of a third-party js script doesn't affect the overall loading of a page?

If a third-party script is slowing down your page load, you have several options to improve performance: Load the script using the async or defer attribute to avoid blocking document parsing. Consider self-hosting the script if the third-party server is slow.

How can we reduce the impact of third-party codes?

The first step in reducing the impact of third-party code is auditing the scripts on your site to see which are slow. To estimate the potential improvement removing a script will have, Chrome allows specific scripts to be blocked for Lighthouse audits. Go to Network > JS > right click on a script > Block request URL.


1 Answers

First approach, if your slowness is on load, create placeholders for the ads and load them very last after everything else.

Second approach, create a Javascript timer or include a timer library to measure the page load time. If it is greater than your acceptable threshold then kill the ad with Javascript or log the slowness to a web service.

Third approach, if the timer does not pick up the slowness because it is incremental then use a setTimeout function that records a timestamp and calls itself every 200ms and compares the new timestamp each call to the older timestamp from the previous call.

If a setTimeout call set to run at 200ms takes 500ms to finally run then you've got substantial delay and should kill the ad with Javascript or log the slowness to a web service.

Each of these methods will need to be tuned to your actual site.

like image 62
N-ate Avatar answered Oct 11 '22 17:10

N-ate