

Page visibility and performance metrics
source link: https://www.tuicool.com/articles/I3eEVvn
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

When we measure the page loading speed from the user’s perspective, we pay attention to the appearance of subsequent elements on the screen. Metrics such as First Contentful Paint, First Meaningful Paint and Visually Complete directly reflect what the user sees and when. But what if the page is invisible, when it loads in the background, for example in a different tab? Should we consider such views interesting for us? Don’t the collected metrics distort the results?
Methodology
To answer these questions, we first need information about whether the page was visible when it was loading. For this purpose, we decided to send new pieces of information from the browsers, collected by the Page Visibility API:
- Initial page visibility state
- Change of page visibility
Each time the tab is switched, hidden or the browser window is displayed (including switching the desktop in the operating system), it is recorded.
By comparing time of visual metrics with time of visibility changes, we can split page views into two buckets:
- Visible all the time until Visually Complete
- Invisible or partially invisible while loading
Results
After splitting metrics in the way described above, we can compare the number of page views of each type and the values of the main metrics collected for them. We did this for three Allegro pages: home page, offers listing and the offer page. The one loaded in the background the most was the offer page, then homepage and last — listing page. This seems to correspond to the scenarios of using these pages — e.g. when browsing search results, users open multiple offers in the background to compare them with each other.
Many more views took place in the background for desktops than for smartphones. This data is not surprising, either, since on the phone it is much less convenient to use websites in many tabs.
For smartphones, First Contentful Paint time turned out to be several hundred seconds (!) higher for pages loading in the background than for visible pages. For desktops, it was much less, but still over 100 seconds.
When we excluded an error in the aggregation algorithm, we looked at how to we collect metrics. To better approximate the moment when content appears on the screen, we used to use the requestAnimationFrame
API, which has been available for a long time in all major browsers. This function allows you to plug in our own code into the browser rendering process, just before the work of drawing the content. At this very moment — when the interesting part of HTML was parsed, but before showing it on the screen — we send information to the server. Unfortunately, currently there is no browser API that would allow code to be executed right after the visible content has been updated.
<div id="meaningul-content-here"></div> <script> function sendMark() { window.performance.mark('FirstMeaningfulPaint'); } requestAnimationFrame(sendMark); </script>
It turned out that in order to optimize the use of hardware resources, the browser does not render anything for tabs in the background. Even though it still downloads stylesheets and parses HTML, it omits calculation of elements’ dimensions (so called Layout) and drawing them. There is no animation frame rendered, thus the code passed to requestAnimationFrame
is not executed. This stage of work is postponed until the tab is activated. The reporting function starts almost at the same time as sending information about the change of visibility of the page. We have experimentally proved this hypothesis, and it also found confirmation in the collected data. First Paint — a metric reported by the browser itself — is also very high for hidden tabs. This means the first frame render occcurs when a tab becomes active.
In order not to bias the results, we decided to conditionally — for invisible pages — collect metrics related to drawing at the time of parsing the HTML code of elements. We are aware that this is a distortion in the opposite direction (measured time is smaller than it should be), but the expected difference is much smaller than when using requestAnimationFrame
.
<div id="meaningul-content-here"></div> <script> function sendMark() { window.performance.mark('FirstMeaningfulPaint'); } if (document.visibilityState != 'visible') sendMark(); else requestAnimationFrame(sendMark); </script>
After implementing the patch and re-checking the data, the picture changed significantly, but the trend remained — invisible pages load noticeably slower and their higher times affect the overall result.
At first glance, this might seem illogical — since the browser does not render the content of invisible tabs, i.e. it does less work for them, loading should end faster. Again, it is about optimizing utilization of resources and giving higher priority to the active tab. For pages loading in the background, the browser limits not only access to the CPU but also to network resources. In this way, the resources of inactive tabs download more slowly, which results in increased loading times.
Summary
While we optimize the front-end we focus primarily on the speed of delivering content to the user. We should pay attention to the pages someone is looking at and ignore those loaded in the background. We will treat the visibility of the page during the loading process as another dimension on our dashboards and we will focus mostly on the measures collected from active tabs. We believe that data from inactive tabs should be considered unnecessary and distorting the picture.
Recommend
-
85
除非特别声明,此文章内容采用知识共享署名 3.0许可,代码示例采用Apache 2.0许可。更多细节请查看我们的服务条款。
-
63
一、简介 有时候,开发者需要知道,用户正在离开页面。常用的方法是监听下面三个事件。 pagehide beforeunload unload 但是,这些事件在手机上可能不会触发,页面就直接关闭了。因为手机系统可以将一个...
-
7
content-visibility: the new CSS property that boosts your rendering performance Improve initial load time by skipping the rendering of offscreen content.
-
21
Page Simulation for Better Offline Metrics at Netflixby David Gevorkyan, Mehm...
-
10
Pixel Position and Visibility DistributionAmong plenty of other things, 2020 has been the year when tracking search engine rankings took a turning point.Debates started to grow intense on whether rankings, as we knew them, are still re...
-
3
The Page Visibility API in Javascript It seems there is a Page Visibility API that will let us trigger actions based on the page tab begging active...
-
6
By:
-
11
GL #9: Caffeine and exercise performance (30 page study summary)tibi2 hr ago...
-
3
I am always excited to share insights about lesser-known yet powerful APIs in the modern web ecosystem. Today, we'll dive into the Page Visibility API. This invaluable tool helps developers optimize their web applications by detecting whe...
-
6
These PR trade tips can help your brand land big placements and reap valuable search discovery and revenue benefits.
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK