GoogleChrome / web-vitals-extension

A Chrome extension to measure essential metrics for a healthy site
https://chrome.google.com/webstore/detail/web-vitals/ahfhijdlegdabablpippeagghigmibma?hl=en
Apache License 2.0
2.35k stars 105 forks source link

the extension's injected `toLocaleString` can cause long tasks in the page's main thread #107

Closed brendankenny closed 1 year ago

brendankenny commented 2 years ago

Describe the bug Formatting metric timestamps with toLocaleString() can cause a long task in the target page's main thread.

To Reproduce

It's not a long task on my machine, but for example:

DevTools perf panel showing a 12 millisecond getTimestamp task from web-vitals.js

(@gilbertococchi who spotted this issue saw a 74ms task)

This points to getTimestamp and its call to toLocaleTimeString

https://github.com/GoogleChrome/web-vitals-extension/blob/a938095d13fca49090f58a8186a065b717c6c4cc/src/browser_action/vitals.js#L189-L192

Loading the formatter is inherently slow, though the browser will cache simple formatters like this for follow up calls. This actually happens here; the second call to broadcastMetricsUpdates is much faster because it's using a cached formatter:

DevTools perf panel showing a 66 microsecond getTimestamp task from web-vitals.js

Expected behavior The extension shouldn't cause long tasks itself.

Unfortunately there's not a way around loading the formatter at least once to get a formatted value. The value would need to either not be formatted, or maybe it could be formatted in the popup (here?), which should happen outside of the page's process.

Version: