We have an API which randomly takes high content download
time in chrome, It works fine always in firefox and takes an only few ms
. The response size is 20kb uncompressed and 4kb compressed. The same request also works fine using curl.
Things that we have tried:
If-None-Match
header to disable cache response from the browser.The same request works fine sometimes on chrome but randomly returns very high content download time.
We are unable to understand the root cause of this issue. What are the other things we can try to minimize this time?
I made three requests here and the 3rd one took the most time (before the last spike). CPU does not seem to be maxing out for a longer period of time. Most of the time is idle time.
Also, When replaying the call using Replay XHR
menu, the Content download period drops from 2s to 200 ms.
"Content Download" measures how long it took to download the response to the HTTP request after the latency is over. There is more information available in the documentation.
How to increase download speed in Chrome by disabling or removing extensions: Open Chrome. Type chrome://extensions into address bar and hit Enter. Find the unused extensions from the list. Toggle the switch to Off or click on the Remove button. *4. Clear Browsing Data Open Chrome. Click on the three dots button.
I was checking for XHR calls timing in Chrome DevTools to improve slow requests but I found out that 99% of the response time is wasted on content download even though the content size is less than 5 KB and the application is running on localhost (Working on my local machine so no Network issues).
Chrome is one of the fastest and most widely used browsers on the market. However, there may be times when hidden bugs and issues slow it down. After going through this article, you can determine if it's the internet or your browser that's slowing down your downloads.
When going directly to a resource or attempting to update rendered content as the result of a web call, the content download time can take up to 10x the duration when made from other client applications or when simply saving the data off as a variable in Chrome.
Are you by chance trying to implement infinite scrolling? If you are, try dragging the scroll bar instead of using the mouse wheel. For some reason, Chrome seems to struggle with mouse scroll events. If the scroll bar worked just fine, keep reading.
This post provides a detailed walkthrough of someone experiencing something similar - https://github.com/TryGhost/Ghost/issues/7934
I had attached a watcher on the scroll
event which would trigger an AJAX request. I had throttled the request and could see that only 1 was being sent. I watched my dev server return the response within a few ms but there would be a 2 second delay in chrome. No render, no api calls, no and scripts executing. But the "Content Download" would take 3 seconds for 14kb. No other browser had this issue.
I stumbled upon suggestions that using requestAnimationFrame
instead of setTimeout
would solve the problem. That approach seems that approach works when the "Waiting" or green is significant, not so much for the "Content Download" or blue.
After hours of digging, I tried conditionally calling e.preventDefault()
on the mousewheel
event and to my amazement, it worked.
A few things to note:
1) I did not use the mousewheel
event to make the api call. I used the scroll
event along with throttling.
2) The mousewheel
event is non-standard and should not be used. See https://developer.mozilla.org/en-US/docs/Web/Events/mousewheel
3) BUT in this case, you have to watch and handle the mousewheel
event because of chrome. Other browsers ignore the event if they don't support it and I have yet to see it cause an issue in another browser.
4) You don't want to call preventDefault()
every time because that disables scrolling with a mouse :) You only want to call it when deltaY
is 1 if you are using vertical scroll. You can see from the attached image that deltaY
is 1 when you basically can't scroll anymore. the mousewheel
event is fired even though the page cannot scroll. As a side note, deltaX
is -0 when you are scrolling vertically and deltaY
is -0 when scrolling horizontally.
My solution:
window.addEventListener("mousewheel", (e) => {
if (e.deltaY === 1) {
e.preventDefault();
}
})
That has been the only solution that I've seen work and I haven't seen it mentioned or discussed elsewhere. I hope that helps.
console log of mousewheel event
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With