重度抑郁症吃什么药| 扭转乾坤什么意思| 12月14号是什么星座| 腰胀是什么原因引起的| 西米是什么米| 1999年是什么生肖| 心脏不舒服有什么症状| 鸡精吃多了有什么危害| 那天离开你是什么歌| 什么药治便秘效果最好最快| mra是什么药| miko是什么意思| 睡觉趴着睡什么原因| 备孕吃什么最容易怀孕| 什么叫轻度脂肪肝| 有什么笑话| 年字五行属什么| 产妇月子里可以吃什么水果| 质体是什么| 左脸颊长痘是什么原因| 118号是什么星座| 溃疡是什么病| 7月22日什么星座| 血糖有点高吃什么食物好| 点数是什么意思| hrd阳性是什么意思| 晚上七点到九点是什么时辰| 维生素a是什么| 清真是什么意思| 力不到不为财是什么意思| fu什么意思| 抗心磷脂抗体是什么意思| 检查包皮挂什么科| 锚什么意思| ai是什么元素| 血糖高能喝什么粥| 血常规五项能检查出什么病| 供给侧改革什么意思| 牛仔蓝是什么颜色| 十二指肠霜斑样溃疡是什么意思| 流金岁月什么意思| 鸡蛋饼用什么面粉| 什么中药补气血效果最好| sds是什么| 牙齿遇冷热都痛是什么原因| 女人脚发热是什么原因| 绿色大便是什么原因| 梦见很多虫子是什么意思| 北加田加共是什么字| 狗狗能看见什么颜色| 蚊子爱咬什么样的人| 健脾益气是什么意思| 孙悟空头上戴的是什么| 外阴白斑是什么症状| 阴阳和合是什么意思| 刘备代表什么生肖| 烧仙草是什么| 葸是什么意思| 手指麻木是什么原因| 二个月不来月经是什么原因| 血管瘪是什么原因| 避孕药吃了有什么副作用| 低压太低是什么原因| 双子座有什么特点| 什么样的红点是白血病| 傀儡什么意思| 并发是什么意思| 子宫肌瘤吃什么药| 迁移是什么意思| 股骨头坏死挂什么科| 12580是什么号码| 桑叶泡水喝有什么好处| 什么是引流| 什么是纳米| 吃什么回奶最快最有效| 什么是数据标注| 血压的低压高是什么原因| 体育精神是什么| 脸麻是什么原因| 丝瓜络有什么作用| 夜未央什么意思| 甲状腺1类是什么意思| 多吃海带有什么好处和坏处| 眼球突出是什么原因| 江米是什么米| 做病理是什么意思| 元旦唱什么歌| 无回声结节是什么意思| 什么叫逻辑思维| 现在可以种什么农作物| 步步生花是什么意思| 奇妙的什么| 感光是什么意思| 肠粉是用什么粉做的| 张飞为什么不救关羽| 宜余事勿取是什么意思| 伟哥是什么药| 什么是蜘蛛痣图片| 陶渊明是什么派诗人| 甲状腺双叶结节什么意思| 南瓜和什么不能一起吃| 伊丽莎白雅顿什么档次| 胃不好吃什么最养胃| 良去掉一点读什么| 11月1日什么星座| 什么样的闪电| 抖s是什么意思| 石斛是什么| 舌尖疼吃什么药| 做梦捡到钱了什么预兆| 什么是指标到校| 六月26日是什么日子| 莘字五行属什么| 吃苹果有什么好处和功效| 陈皮有什么作用| 推介会是什么意思| 散光是什么意思| 胆囊炎吃什么药好得快| 保鲜卡是什么原理纸片| 宫代表什么生肖| 什么叫人均可支配收入| 三个毛念什么| 血压偏高是什么原因| 知否知否应是绿肥红瘦什么意思| 什么血型容易被蚊子咬| 宝宝什么时候开始长牙| pth是什么| 壁虎是什么动物| 切屏是什么意思| 关东煮为什么叫关东煮| 头疼吃什么药效果好| 百雀羚适合什么年龄段| 林是什么生肖| 00年属什么| 屁股下垂穿什么裤子| 紫藤什么时候开花| 双肺索条灶是什么意思| 占有欲强什么意思| 治疗阴虱子用什么药最好| 手足口病涂抹什么药膏| 脚心疼是什么原因| 薄荷泡水喝有什么功效| 喉咙细菌感染吃什么药| 梦见蛇挡路是什么意思| 肝占位病变是什么意思| 什么是角阀| 早年晚岁总无长是什么意思| 高兴的反义词是什么| 没有润滑油用什么代替| 低钠盐是什么意思| 孕吐最早什么时候开始| 戒断是什么意思| 质子治疗是什么意思| 什么细节能感动摩羯男| 糖尿病是什么原因引起的| 心跳过缓是什么原因造成的| 梦见虱子是什么意思| 观音坐莲是什么姿势| 痛风是什么原因引起的| 癔病是一种什么病| 1975年属兔五行属什么| 79年属什么的生肖| 性张力是什么意思| 五台山是什么菩萨的道场| 好老公的标准是什么| 明目张胆是什么生肖| 耳塞戴久了有什么危害| 世界上最大的山是什么山| 给朋友送什么礼物好| 细菌性痢疾症状是什么| 刮痧不出痧是什么原因| ab型血和b型血的孩子是什么血型| 17岁属什么生肖| sheep什么意思| 搁浅了是什么意思| 为什么尿黄| 伶人是什么意思| 办离婚需要什么手续和证件| 农历4月是什么月| 玉髓是什么| 大将军衔相当于什么官| 四月四号什么星座| 什么叫伴手礼| 空腹吃荔枝有什么危害| 金骏眉茶是什么茶| 做肠镜前喝的是什么药| 怕老婆的男人说明什么| 缴费基数是什么意思| 剪发虫是什么| 月柱华盖是什么意思| 特长是什么| 嗓子干痒咳嗽吃什么药| 什么叫周围神经病| 儿保做些什么检查项目| 9月25日是什么星座| goldlion是什么牌子| 夏至为什么要吃面条| 多出汗有什么好处| 肺部ct应该挂什么科| 益生菌什么时候吃| 四字五行属什么| 什么是横纹肌溶解| comeon什么意思| 假借是什么意思| 白带过氧化氢阳性什么意思| 肝胃郁热吃什么中成药| 梅雨季节是什么意思| 脖子长痘是什么原因引起的| 东北属于什么气候| 木鱼花是什么做的| 鱼鳞云代表什么天气| 上焦不通吃什么中成药| 幻觉幻听是什么症状| 夏雨什么| 胜字五行属什么| 霜对什么| 梦见猫吃老鼠什么意思| 肚脐眼上面痛是什么原因引起的| 肿气肿用什么药比较好| 纷乐是什么药| 尿隐血2十是什么原因| 大姨妈吃什么水果| 宫颈液基细胞学检查是什么| 飞机打多了会有什么后果| 梦见自己的车丢了是什么意思| 盆腔炎吃什么药| 毛主席什么时候去世| 大本营是什么意思| 什么什么于怀| hpv58阳性是什么意思| 扁平疣用什么药膏| 血色病是什么病| 1966年属马的是什么命| 运筹帷幄是什么意思| prp是什么意思| 眼睛淤青用什么方法能快点消除| 气球是什么意思| 2022年属什么生肖| 1905年属什么生肖| 西夏国是现在什么地方| 妇科湿疹用什么药膏最有效| 什么是云母| 脚后跟干裂用什么药膏| 六月十六什么星座| 一个白一个本是什么字| 每天早上起床口苦是什么原因| 口腔溃疡吃什么水果好| 下午5点多是什么时辰| 廿年是什么意思| 佝偻病是缺什么| 哲理是什么意思| 为什么冰箱冷藏室会结冰| ad吃到什么时候| 32周岁属什么生肖| 打胎后要注意什么| 拉架棉是什么面料| 宝宝囟门什么时候闭合| 凤辇是什么意思| 短pr间期是什么意思| 为什么做完爱下面会疼| 9月13日是什么纪念日| 尿很臭是什么原因| 血管瘤是什么病严重吗| 梦见花开是什么预兆| 百度
Fix JavaScript performance title. For modern web apps subtitle. The background image is a path through a forest.

成都举行绿道踏青大会

Author avatarDebugBear9 minute read
百度 中国——明十三陵是中国明朝皇帝的墓葬群,坐落在北京郊区境内的燕山山麓的天寿山。

When it comes to web performance, you may think of techniques like compression, asset optimization, or even HTTP caching. These are really important, and there's plenty of existing resources covering ways to fix or implement them. However, some less-discussed performance bottlenecks can severely impact website speed. In this post, we'll discuss three issues that often originate from inefficient JavaScript patterns:

  • Long tasks: JavaScript operations that monopolize the main thread, leading to unresponsive user interfaces.
  • Large bundle sizes: JavaScript code that's too big to download, parse, and execute quickly.
  • Hydration issues: The process of attaching JavaScript functionality to server-rendered HTML.

While these are not new problems, modern web practices and web frameworks can exacerbate these issues, putting them in the spotlight once again.

Note: These issues can also stem from other sources, such as CSS. For example, a long task can be caused by a complex CSS selector that takes a long time to match elements. However, the focus of this post is on JavaScript.

Here's more information on each bottleneck, along with high-level commentary on addressing such performance issues.

Long tasks

Long tasks occur when there's constant main thread activity that blocks the browser for 50ms or more. Note that many JavaScript and browser rendering tasks are performed sequentially while on the main thread.

When the main thread is busy, it cannot handle user interactions, or do other important rendering tasks. This can lead to noticeable delays in page responsiveness.

Long tasks on a request waterfall chart. The long tasks affect page rendering

Let's run a website speed test on a Next.js page. This page is server-rendered and then hydrated on the client. Interestingly, the page shows that the user saw useful content quite early on, in spite of the large HTML payload.

The lab-based test that captures this page load shows an acceptable Largest Contentful Paint (LCP) time, and no Cumulative Layout Shift (CLS). This simplified diagram shows what happens when a user interacts during a long task.

User interaction during a long task can delay the page updating

The problem here is that even content may be loaded in a timely manner, the page was not interactive for a long time as the browser was busy parsing and executing the JavaScript. During the slow hydration process, the user saw content that appeared to be interactive, but was not. This meant that the page appeared 'frozen' while the user was trying to interact with it.

This example is particularly interesting because it puts emphasis on parse and execution times, not just download times.

How to shorten your long tasks

  1. Yield to the main thread: Break up your work into smaller, manageable tasks. This effectively gives the browser a break, allowing it to handle user interactions and rendering. You can learn more about this technique in our article on using the Scheduler API.

  2. Use Web Workers: While it might not make your code run faster, Web Workers allow you to run JavaScript in the background, keeping your main thread free for user interactions and rendering.

  3. Optimize your rendering patterns: Implement techniques like:

As a general performance strategy, you should minimize main thread work and activity.

Large bundle sizes

A large bundle size refers to the total amount of JavaScript code that needs to be downloaded, parsed, and executed when a user visits your website. The 'bundle' is a JavaScript file that contains your application code, along with any dependencies. Oversized JavaScript bundles can lead to several issues:

  • Lower cache hit rates: Large files are more likely to be invalidated and re-downloaded because they are more likely to change. This reduces the benefits of caching.
  • Slower download times: Larger files take longer to download, especially on slower connections.
  • Increased parsing and execution times: This one is easily overlooked: even on a fast network connection, parsing and executing large JavaScript files can take a significant amount of time, especially on mobile devices. Slow parse and execution times also contribute to long tasks.

The last point is also an issue for user interactions. Users interact with pages expecting immediate feedback, but if the browser is busy parsing and executing JavaScript, it can't respond to user input.

A page with two large JavaScript bundles affecting page rendering

The previous screenshot shows a page that has a number of large JavaScript bundles. The page rendering is blocked by:

  • The download time of the JavaScript bundles.
  • The parser-blocking nature of the JavaScript bundles.
  • Execution time of the JavaScript bundles.

How to address large bundle sizes

  • Implement tree shaking to remove unused code from your final bundle.
  • Use code splitting to break your application into smaller chunks that can be loaded selectively.
  • Use lazy loading to defer the loading of non-critical resources until they're needed.
  • Remove unused code by using the Coverage Panel in Chrome DevTools or Edge DevTools to identify and eliminate unused code. Coverage is compatible with source maps so you can understand exactly what source code file is unused, and can be safely removed/deferred.
  • Move to native web platform features where possible, reducing the need for custom JavaScript code. Many common patterns and features that were historically implemented from scratch using JavaScript now have native browser support. This means you can achieve the same functionality with less code.

Here are some powerful web platform features that can replace common JavaScript implementations:

And here are some additional web platform features to be aware of. You can start to try these out, but be mindful that they don't have cross-browser support yet. If you use these features, you should check that your website continues to be usable on browsers which don't yet support such features.

Hydration issues

JavaScript bundle sizes used to be easier to understand. Developers knew what went into them; they consisted of the developer's own code, the framework's code, and third-party library code. All these parts were combined into one file (like app-v123.js) during the build process. This made it simpler to identify what was causing large file sizes.

However over time, JavaScript web frameworks introduced server-side rendering. This new approach created a different kind of HTML response, leading to hydration issues.

What is hydration in web development?

Hydration is the process of attaching JavaScript functionality to server-rendered HTML, making the HTML interactive on the client-side. Popular frameworks like Next.js use certain hydration techniques by default.

While hydration can complement server-side rendering (SSR) capabilities, it can introduce performance challenges:

  • Increased document size: Some frameworks serialize state into the HTML source as JSON, bloating the initial payload and even duplicating data. In addition, the inlined serialized state carries a parse and execution cost, as discussed earlier.
  • Interactivity delays: Users may experience a frustrating period where the page is visible but not yet interactive. The larger the hydration payload, the longer this delay is. This is sometimes referred to as the uncanny valley.
  • Wasteful rebuilding: Some JavaScript frameworks offering SSR functionality effectively lead to the DOM being built twice – once on the server and again on the client during hydration. This redundancy can waste resources and slow down the time to interactivity.

You should always consider the impact of popular JavaScript frameworks and libraries on performance. The abundance of resources, tutorials, and starter kits for popular frameworks can sometimes lead developers to choose them without fully considering the performance implications.

For some web developers, creating a website with a JavaScript framework is the default choice, no matter the use case. Framework popularity does not always translate to better UX, so you must balance developer ergonomics, maintainability, and user experience.

Size analysis of a Next.js page showing hydration issues

Debugbear's HTML Size Analyzer is particularly useful as it breaks down the size of the HTML document, showing the size of the initial HTML payload and the size of the hydration payload. The previous screenshot shows that the large HTML document is largely due to:

  • 50kb of paragraph text (the content the user sees first)
  • 50kb of JSON data (the hydration payload)

It's not a coincidence that the JSON data is the same size as the paragraph text. This is because the JSON data is a serialized version of the paragraph text. This duplication is a common issue with hydration payloads.

Strategies for mitigating hydration issues

Here are some avenues you can explore to improve the performance of web apps that have issues with hydration. You should be aware that using one of these techniques can involve significant architectural changes, and in some cases, can involve moving to a different JavaScript framework.

  1. Progressive hydration / Selective hydration: Prioritize hydrating the most critical parts of your application first, deferring less important components until later.

  2. Islands architecture: Implement "islands" of interactivity within a sea of static content, reducing the overall hydration cost. With islands architecture, you typically use directives to mark which islands should be hydrated.

  3. Resumability: Instead of rebuilding the entire DOM, explore frameworks that can "resume" the server-rendered state more efficiently. At times, almost no JavaScript is sent down during page load, but the necessary JavaScript is fetched and executed when needed, such as on user interaction.

Server-side rendering

Consider ditching complex frameworks, and instead use a server-side rendered application with vanilla client-side JavaScript:

  1. Use regular hyperlinks and form submissions for navigation.
  2. Implement View Transitions API for smooth page transitions.
  3. Add a thin layer of JavaScript only where absolutely necessary.

For example, you can use:

  • Hotwire for basic client-side functionality.
  • Stimulus for more advanced client-side interactions.
  • Turn for smooth transitions between pages and components.

When you combine these tools with effective caching strategies and page preloading, you can create highly performant web applications without sacrificing functionality and user experience.

Measure how JavaScript performance impacts real users

The Interaction to Next Paint (INP) metric measures how quickly a website responds to user input. If the page freezes for half a second after an interaction, then the page will not feel responsive to the user. INP can be calculated in the browser using the Event Timing API or using a library like web-vitals.js.

When an interaction causes rendering delays, the Long Animation Frames API can tell you what specific scripts were responsible. Every time there's a rendering delay a PerformanceEntry with the type long-animation-frame is recorded. You can access these events like this:

js
performance.getEntriesByType("long-animation-frame");

The invokerType tells you why the script was running. In the example below the value is classic-script, which means the processing occurred during the initial evaluation of the script file.

Screenshot of the browser console showing long animation frames, with the script invoker type and source URL highlighted.

DebugBear real user monitoring can visualize this data for visitors on your website, showing when different scripts were running during the interaction.

You can also see what page element the user interacted with and when the interaction occurred. If the user interacts with a page soon after loading this often causes additional delays, as some parts of the page are still loading, which will delay event handlers.

Screenshot showing performance data for an INP interaction, with INP score and LoAF scripts on the left and the CSS selector and screenshot of the INP element on the right.

Continuously monitoring INP scores on your websites gives you insight into what pages on your website often display poor interaction responsiveness, and how different visitor groups have experienced your website over time. Based on the Long Animation Frames data you can also see scripts are most often responsible for interaction delays.

Screenshot of DebugBear real user monitoring dashboard showing INP data.

Summary

This post explored three lesser-known performance bottlenecks caused by excessive JavaScript: long tasks, large bundle sizes, and hydration issues. We examined how web frameworks can sometimes amplify these problems, resulting in slower load times and negatively impacting user interactions. Finally, we discussed approaches to mitigate these bottlenecks and what you can do to measure how slow JavaScript impacts your visitors.

We hope you found this post helpful and that it inspires you to build more efficient, high-performing websites that provide a smoother experience for users.

This post is sponsored by DebugBear. DebugBear helps website developers deliver a better user experience and pass Google's Core Web Vitals assessment. Get detailed page speed recommendations and continuously monitor synthetic and real user page speed metrics.

五脏六腑什么意思 神经损伤吃什么药 大什么大 指鼻试验阳性代表什么 欧什么意思
社保断了有什么影响 fq交友是什么意思 山楂和什么泡水喝降血压 质询是什么意思 香蕉补什么
分割线是什么意思 不撞南墙不回头是什么意思 胯骨在什么位置 peek是什么材质 腺样体肥大是什么症状
不来月经是什么原因 林冲是什么生肖 梨形心见于什么病 痛风能吃什么东西 老人脚肿是什么征兆
职业测试你适合什么工作hcv9jop7ns3r.cn 吃过饭后就想拉大便是什么原因hcv7jop9ns4r.cn 缺维生素d有什么症状hcv7jop4ns8r.cn 口腔长期溃疡是什么原因引起的hcv9jop5ns9r.cn anca是什么检查hcv8jop3ns8r.cn
徐州菜属于什么菜系hcv9jop2ns3r.cn 1月出生是什么星座hcv9jop3ns0r.cn 心路历程是什么意思hcv9jop2ns7r.cn 妥协是什么意思hcv7jop7ns4r.cn 人丹是什么药hcv8jop4ns9r.cn
孕妇快生的时候有什么征兆fenrenren.com 过敏性鼻炎吃什么药好hcv9jop3ns0r.cn 关联词是什么意思hcv8jop3ns2r.cn 风疹病毒igg阳性是什么意思hcv7jop6ns4r.cn 乌唇是什么原因hcv8jop3ns3r.cn
家里出现蟑螂预示什么hcv8jop2ns9r.cn 约稿是什么意思hcv8jop1ns3r.cn 网是什么结构的字hcv9jop6ns6r.cn bmr是什么意思hcv8jop4ns6r.cn 外感发热什么意思hcv9jop8ns3r.cn
百度