WebPerf: Resources & Network
JavaScript snippets for measuring web performance in Chrome DevTools. Execute with mcp__chrome-devtools__evaluate_script, capture output with mcp__chrome-devtools__get_console_message.
Scripts
- -
scripts/Network-Bandwidth-Connection-Quality.js — Network Bandwidth & Connection Quality
Descriptions and thresholds: INLINECODE3
Common Workflows
Network Quality Assessment
When the user asks about network performance, connection quality, or adaptive loading:
- 1. Network-Bandwidth-Connection-Quality.js - Analyze network bandwidth, effective connection type, RTT, downlink, save-data mode
Adaptive Loading Strategy
When implementing adaptive loading or the user asks "how to optimize for slow connections":
- 1. Network-Bandwidth-Connection-Quality.js - Detect connection quality
- Cross-reference with webperf-loading skill:
- TTFB.js (measure server response on slow connections)
- Find-render-blocking-resources.js (identify heavy resources to defer)
- Resource-Hints-Validation.js (optimize preconnect for slow networks)
- 3. Cross-reference with webperf-media skill:
- Image-Element-Audit.js (implement responsive images based on connection)
- Video-Element-Audit.js (adjust video quality based on connection)
Save-Data Mode Detection
When the user asks about save-data or data-saving features:
- 1. Network-Bandwidth-Connection-Quality.js - Check if save-data is enabled
- If save-data is enabled, recommend:
- Reducing image quality
- Disabling autoplay videos
- Deferring non-critical resources
- Using low-res thumbnails
Mobile/Slow Connection Optimization
When the user asks "optimize for mobile" or "slow connection users":
- 1. Network-Bandwidth-Connection-Quality.js - Assess connection type
- Cross-reference with webperf-loading skill:
- TTFB.js (critical for slow connections)
- Find-render-blocking-resources.js (minimize blocking on slow networks)
- Critical-CSS-Detection.js (inline critical CSS to reduce RTT)
- Prefetch-Resource-Validation.js (avoid excessive prefetch on slow connections)
Decision Tree
Use this decision tree to automatically run follow-up snippets based on results:
After Network-Bandwidth-Connection-Quality.js
- - If effectiveType is "slow-2g" or "2g" → Very slow connection, recommend:
1. Run
webperf-loading:Critical-CSS-Detection.js (inline critical CSS)
2. Run
webperf-media:Image-Element-Audit.js (implement aggressive lazy loading)
3. Run
webperf-loading:Prefetch-Resource-Validation.js (remove prefetch to save bandwidth)
4. Recommend minimal resource strategy
- - If effectiveType is "3g" → Moderate connection, recommend:
1. Run
webperf-loading:Find-render-blocking-resources.js (minimize blocking)
2. Run
webperf-media:Image-Element-Audit.js (responsive images)
3. Run
webperf-loading:Resource-Hints-Validation.js (optimize preconnect)
4. Implement adaptive image quality
- - If effectiveType is "4g" or better → Good connection, recommend:
1. Standard optimization practices
2. Consider strategic prefetch for navigation
3. Higher quality media is acceptable
- - If save-data is enabled → User explicitly wants to save data, recommend:
1. Reduce image quality aggressively
2. Disable autoplay videos
3. Defer non-critical resources
4. Remove prefetch/preload hints
5. Show "high quality" toggle option
- - If RTT > 300ms → High latency, recommend:
1. Run
webperf-loading:TTFB.js (latency impacts TTFB)
2. Run
webperf-loading:Resource-Hints-Validation.js (preconnect critical for high RTT)
3. Minimize number of origins
4. Use HTTP/2 or HTTP/3 for multiplexing
- - If downlink < 1 Mbps → Very limited bandwidth, recommend:
1. Run
webperf-media:Image-Element-Audit.js (aggressive compression)
2. Run
webperf-media:Video-Element-Audit.js (disable autoplay)
3. Run
webperf-loading:Prefetch-Resource-Validation.js (remove prefetch)
4. Implement bandwidth-aware loading
- - If downlink > 10 Mbps → Good bandwidth, consider:
- Higher quality media
- Strategic prefetch
- Preloading next-page resources
Cross-Skill Triggers
These triggers recommend using snippets from other skills:
From Resources to Loading Skill
- - If slow connection detected (2g/3g) → Use webperf-loading skill:
- TTFB.js (latency is amplified on slow connections)
- Critical-CSS-Detection.js (reduce RTT by inlining critical CSS)
- Find-render-blocking-resources.js (minimize blocking resources)
- Resource-Hints-Validation.js (preconnect is critical for high RTT)
- Prefetch-Resource-Validation.js (avoid prefetch on slow connections)
- - If high RTT detected (>200ms) → Use webperf-loading skill:
- TTFB-Sub-Parts.js (break down latency components)
- Resource-Hints-Validation.js (preconnect to reduce RTT impact)
- Service-Worker-Analysis.js (caching is critical for high latency)
From Resources to Media Skill
- - If slow connection or save-data detected → Use webperf-media skill:
- Image-Element-Audit.js (implement responsive images, aggressive compression)
- Video-Element-Audit.js (disable autoplay, reduce quality)
From Resources to Core Web Vitals Skill
- - If slow connection detected → Check Core Web Vitals impact:
- Use
webperf-core-web-vitals:LCP.js (LCP is heavily impacted by slow connections)
- Use
webperf-core-web-vitals:INP.js (high latency can impact interaction responsiveness)
Adaptive Loading Implementation Guide
Based on Network Information API results, implement these strategies:
For slow-2g / 2g (< 50 Kbps):
CODEBLOCK0
For 3g (50-700 Kbps):
CODEBLOCK1
For 4g+ (> 700 Kbps):
CODEBLOCK2
For save-data mode:
CODEBLOCK3
Performance Budget by Connection Type
Adjust performance budgets based on connection quality:
slow-2g / 2g:
- - Total page weight: < 500KB
- Images: < 200KB total
- JavaScript: < 100KB total
- No videos
3g:
- - Total page weight: < 1.5MB
- Images: < 800KB total
- JavaScript: < 300KB total
- Videos: < 3MB (only if critical)
4g+:
- - Total page weight: < 3MB
- Images: < 2MB total
- JavaScript: < 1MB total
- Videos: < 10MB
Real-World Scenarios
Scenario: User on 3G reports slow page load
- 1. Run Network-Bandwidth-Connection-Quality.js → confirms 3g
- Run webperf-loading:TTFB.js → high TTFB due to latency
- Run webperf-loading:Critical-CSS-Detection.js → CSS not inlined
- Recommendation: Inline critical CSS, implement adaptive loading
Scenario: User with save-data enabled complains about data usage
- 1. Run Network-Bandwidth-Connection-Quality.js → saveData: true
- Run webperf-media:Image-Element-Audit.js → high-res images served
- Run webperf-media:Video-Element-Audit.js → autoplay enabled
- Recommendation: Respect save-data, reduce quality, disable autoplay
Scenario: International users report slow LCP
- 1. Run Network-Bandwidth-Connection-Quality.js → high RTT (300ms+)
- Run webperf-core-web-vitals:LCP.js → LCP 4s+
- Run webperf-loading:TTFB-Sub-Parts.js → DNS + connection = 500ms
- Recommendation: Use CDN, implement preconnect, optimize for latency
Scenario: Mobile users in rural areas
- 1. Run Network-Bandwidth-Connection-Quality.js → 2g, high RTT, low downlink
- Run webperf-loading:Find-render-blocking-resources.js → many blocking
- Run webperf-media:Image-Element-Audit.js → all images eager-loaded
- Recommendation: Aggressive adaptive loading, inline critical CSS, lazy load all images
Network Information API Limitations
Be aware of API limitations and fallbacks:
API not available:
- - Safari does not support Network Information API
- Fallback: Use TTFB as proxy for connection quality
- Fallback: Device detection (mobile = assume slow)
- Fallback: User preference toggle
API values are estimates:
- - effectiveType is based on recent observations
- Values can change during session
- Re-check periodically for long sessions
Privacy considerations:
- - Some browsers limit precision for privacy
- Values may be rounded or capped
- Consider user privacy when making decisions
Testing Adaptive Loading
To test adaptive loading implementations:
- 1. Use Chrome DevTools Network Throttling
- Run Network-Bandwidth-Connection-Quality.js at each throttling level
- Verify adaptive strategies activate correctly
- Measure Core Web Vitals at each connection speed
- Adjust breakpoints and strategies based on results
Test matrix:
- - Offline
- slow-2g (50 Kbps, RTT 2000ms)
- 2g (250 Kbps, RTT 300ms)
- 3g (750 Kbps, RTT 100ms)
- 4g (4 Mbps, RTT 20ms)
- save-data enabled at each level
References
- -
references/snippets.md — Descriptions and thresholds for each script - INLINECODE5 — Return value schema for interpreting script output
Execute via mcp__chrome-devtools__evaluate_script → read with mcp__chrome-devtools__get_console_message.
WebPerf: 资源与网络
用于在Chrome DevTools中测量Web性能的JavaScript代码片段。通过mcpchrome-devtoolsevaluatescript执行,使用mcpchrome-devtoolsgetconsole_message捕获输出。
脚本
- - scripts/Network-Bandwidth-Connection-Quality.js — 网络带宽与连接质量
描述和阈值:references/snippets.md
常见工作流程
网络质量评估
当用户询问网络性能、连接质量或自适应加载时:
- 1. Network-Bandwidth-Connection-Quality.js - 分析网络带宽、有效连接类型、RTT、下行链路、省流量模式
自适应加载策略
当实现自适应加载或用户询问如何针对慢速连接进行优化时:
- 1. Network-Bandwidth-Connection-Quality.js - 检测连接质量
- 交叉引用webperf-loading技能:
- TTFB.js(测量慢速连接上的服务器响应)
- Find-render-blocking-resources.js(识别需要延迟加载的重资源)
- Resource-Hints-Validation.js(针对慢速网络优化预连接)
- 3. 交叉引用webperf-media技能:
- Image-Element-Audit.js(基于连接实现响应式图片)
- Video-Element-Audit.js(基于连接调整视频质量)
省流量模式检测
当用户询问省流量或数据节省功能时:
- 1. Network-Bandwidth-Connection-Quality.js - 检查省流量模式是否启用
- 如果省流量模式已启用,建议:
- 降低图片质量
- 禁用自动播放视频
- 延迟加载非关键资源
- 使用低分辨率缩略图
移动端/慢速连接优化
当用户询问针对移动端优化或慢速连接用户时:
- 1. Network-Bandwidth-Connection-Quality.js - 评估连接类型
- 交叉引用webperf-loading技能:
- TTFB.js(对慢速连接至关重要)
- Find-render-blocking-resources.js(最小化慢速网络上的阻塞)
- Critical-CSS-Detection.js(内联关键CSS以减少RTT)
- Prefetch-Resource-Validation.js(避免在慢速连接上过度预取)
决策树
使用此决策树根据结果自动运行后续代码片段:
执行Network-Bandwidth-Connection-Quality.js后
- - 如果effectiveType为slow-2g或2g → 非常慢的连接,建议:
1. 运行
webperf-loading:Critical-CSS-Detection.js(内联关键CSS)
2. 运行
webperf-media:Image-Element-Audit.js(实现激进的懒加载)
3. 运行
webperf-loading:Prefetch-Resource-Validation.js(移除预取以节省带宽)
4. 建议最小资源策略
- - 如果effectiveType为3g → 中等连接,建议:
1. 运行
webperf-loading:Find-render-blocking-resources.js(最小化阻塞)
2. 运行
webperf-media:Image-Element-Audit.js(响应式图片)
3. 运行
webperf-loading:Resource-Hints-Validation.js(优化预连接)
4. 实现自适应图片质量
- - 如果effectiveType为4g或更好 → 良好连接,建议:
1. 标准优化实践
2. 考虑为导航进行策略性预取
3. 可接受更高质量的媒体
- - 如果省流量模式已启用 → 用户明确希望节省数据,建议:
1. 激进地降低图片质量
2. 禁用自动播放视频
3. 延迟加载非关键资源
4. 移除预取/预加载提示
5. 显示高质量切换选项
- - 如果RTT > 300ms → 高延迟,建议:
1. 运行
webperf-loading:TTFB.js(延迟影响TTFB)
2. 运行
webperf-loading:Resource-Hints-Validation.js(预连接对高RTT至关重要)
3. 最小化源站数量
4. 使用HTTP/2或HTTP/3进行多路复用
- - 如果下行链路 < 1 Mbps → 非常有限的带宽,建议:
1. 运行
webperf-media:Image-Element-Audit.js(激进压缩)
2. 运行
webperf-media:Video-Element-Audit.js(禁用自动播放)
3. 运行
webperf-loading:Prefetch-Resource-Validation.js(移除预取)
4. 实现带宽感知加载
- - 如果下行链路 > 10 Mbps → 良好带宽,考虑:
- 更高质量的媒体
- 策略性预取
- 预加载下一页资源
跨技能触发
这些触发建议使用其他技能的代码片段:
从资源到加载技能
- - 如果检测到慢速连接(2g/3g) → 使用webperf-loading技能:
- TTFB.js(延迟在慢速连接上被放大)
- Critical-CSS-Detection.js(通过内联关键CSS减少RTT)
- Find-render-blocking-resources.js(最小化阻塞资源)
- Resource-Hints-Validation.js(预连接对高RTT至关重要)
- Prefetch-Resource-Validation.js(避免在慢速连接上预取)
- - 如果检测到高RTT(>200ms) → 使用webperf-loading技能:
- TTFB-Sub-Parts.js(分解延迟组件)
- Resource-Hints-Validation.js(预连接以减少RTT影响)
- Service-Worker-Analysis.js(缓存对高延迟至关重要)
从资源到媒体技能
- - 如果检测到慢速连接或省流量模式 → 使用webperf-media技能:
- Image-Element-Audit.js(实现响应式图片,激进压缩)
- Video-Element-Audit.js(禁用自动播放,降低质量)
从资源到核心Web指标技能
- - 如果检测到慢速连接 → 检查核心Web指标影响:
- 使用
webperf-core-web-vitals:LCP.js(LCP受慢速连接影响严重)
- 使用
webperf-core-web-vitals:INP.js(高延迟可能影响交互响应性)
自适应加载实施指南
基于网络信息API结果,实施以下策略:
针对slow-2g / 2g(< 50 Kbps):
javascript
// 由effectiveType检测:slow-2g或2g
策略:
- - 提供低分辨率图片(质量:30-40)
- 禁用自动播放视频
- 移除所有预取提示
- 内联所有关键CSS
- 延迟所有非关键JavaScript
- 使用系统字体(无Web字体)
- 激进的懒加载(滚动时加载 + 缓冲区)
针对3g(50-700 Kbps):
javascript
// 由effectiveType检测:3g
策略:
- - 提供中等分辨率图片(质量:60-70)
- 禁用自动播放视频
- 有限预取(仅关键资源)
- 仅内联关键CSS
- 延迟非关键JavaScript
- 预加载1-2个关键字体
- 标准懒加载
针对4g+(> 700 Kbps):
javascript
// 由effectiveType检测:4g
策略:
- - 提供高分辨率图片(质量:80-90)
- 允许自动播放视频(静音)
- 为导航进行策略性预取
- 标准CSS加载
- 标准JavaScript加载
- 预加载关键字体
- 标准懒加载
针对省流量模式:
javascript
// 由navigator.connection.saveData === true检测
策略:
- - 覆盖连接类型,视为比实际更差
- 显示加载高质量切换按钮
- 完全禁用自动播放
- 最少图片,最低质量
- 无预取,无预加载(除关键资源外)
按连接类型的性能预算
根据连接质量调整性能预算:
slow-2g / 2g:
- - 总页面重量:< 500KB
- 图片:< 200KB总计
- JavaScript:< 100KB总计
- 无视频
3g:
- - 总页面重量:< 1.5MB
- 图片:< 800KB总计
- JavaScript:< 300KB总计
- 视频:< 3MB(仅当关键时)
4g+: