从一次线上事故看 IntersectionObserver 与 getBoundingClientRect 的性能差异
前段时间我们后台管理系统的一个长列表页面在低端安卓机上出现了严重的滚动卡顿,用户反馈“滑不动”。排查后发现是列表项在滚动时不断通过 `getBoundingClientRect()` 判断是否进入视口来触发图片懒加载和曝光埋点。这种写法在桌面端没什么问题,但在移动端滚动事件里频繁调用布局属性,每次都强制浏览器重排,直接拖垮了帧率。后来换成 `IntersectionObserver` 后,卡顿消失。这篇文章会从浏览器渲染机制讲起,对比两种方案在滚动场景下的实际开销,给出一套可落地的迁移思路,并说明 `IntersectionObserver` 的 rootMargin、threshold 等配置在懒加载和曝光统计里的具体用法,以及它本身的兼容性边界和降级处理。 ## 先看那段出问题的代码 最初的实现大概长这样: ```js const list = document.querySelector('.list') const items = document.querySelectorAll('.list-item') function checkVisible() { const viewportHeight = window.innerHeight items.forEach(item => { const rect = item.getBoundingClientRect() if (rect.top 0) { loadImage(item) reportExposure(item) } }) } list.addEventListener('scroll', () => { checkVisible() }) ``` 这段代码的逻辑很直白:滚动时遍历所有列表项,拿到每个元素的边界矩形,判断是否和视口相交。问题在于 `getBoundingClientRect()` 会强制浏览器重新计算布局。浏览器为了优化渲染,通常会把 DOM 修改和样式读取攒到一帧里统一处理,但一旦你调用了这类读取几何信息的 API,它就必须立刻把之前所有待处理的样式变更应用掉,再返回准确值。这个动作叫强制同步布局(forced synchronous layout)。 滚动事件触发频率本来就高,再乘上列表项数量,每一帧里都在做全量布局计算,主线程根本忙不过来。低端机上掉帧到十几 fps 是常事。 ## IntersectionObserver 为什么能解决这个问题 `IntersectionObserver` 是浏览器提供的一套异步观察机制。你告诉它要观察哪些元素、以哪个元素作为参照区域、相交到什么程度算“可见”,剩下的交给浏览器在空闲时机去算。它不会阻塞主线程,也不需要你在滚动事件里手动做几何计算。 改写后的代码: ```js const observer = new IntersectionObserver( entries => { entries.forEach(entry => { if (entry.isIntersecting) { const el = entry.target loadImage(el) reportExposure(el) observer.unobserve(el) // 曝光只上报一次就取消观察 } }) }, { root: null, // 默认使用视口 rootMargin: '0px 0px 200px 0px', // 提前 200px 触发,让图片更早开始加载 threshold: 0 } ) document.querySelectorAll('.list-item').forEach(item => { observer.observe(item) }) ``` 这里的 rootMargin 展开是“上右下左”的顺序,`0px 0px 200px 0px` 表示把参照区域的底部向下扩展 200px。这样元素还没真正进入视口时就会触发回调,图片有更充裕的时间加载,用户滑到位置时基本已经就绪。 threshold 表示相交比例。0 代表只要有一个像素相交就触发,1 代表完全可见才触发。做曝光统计时通常会用多个阈值来区分“露出 50%”和“完全露出”,比如 `[0, 0.5, 1]`。 ## 曝光埋点里的一个细节:可见不等于有效曝光 很多业务对曝光的定义是“元素在视口内持续出现超过一定时间”。`IntersectionObserver` 只告诉你相交状态变了,不负责计时。如果直接在 `isIntersecting` 为真时就上报,用户快速滑过去、元素只闪现了几十毫秒也会被算作曝光,数据会虚高。 需要配合定时器做停留判定: ```js const timers = new WeakMap() const MIN_EXPOSURE_TIME = 1000 const observer = new IntersectionObserver(entries => { entries.forEach(entry => { const el = entry.target if (entry.isIntersecting) { const timer = setTimeout(() => { reportExposure(el) observer.unobserve(el) }, MIN_EXPOSURE_TIME) timers.set(el, timer) } else { const timer = timers.get(el) if (timer) { clearTimeout(timer) timers.delete(el) } } }) }) ``` 用 `WeakMap` 存定时器,元素被移除后不会造成内存泄漏。这个模式在信息流、商品列表的曝光统计里很常见。 ## 哪些场景仍然需要 getBoundingClientRect 不是说有了 `IntersectionObserver` 就完全不用几何 API 了。它只能告诉你“是否相交”,拿不到精确的坐标、宽高、偏移量。如果需求是拖拽吸附、自定义 tooltip 定位、判断元素在视口上下左右哪个方向,还是得用 `getBoundingClientRect()` 或 `offsetTop` 之类。关键是要控制调用时机,别把它塞进高频事件里。 比如需要跟随滚动更新某个浮层位置,可以用 `requestAnimationFrame` 把读取操作集中到一帧的开头: ```js let ticking = false window.addEventListener('scroll', () => { if (!ticking) { requestAnimationFrame(() => { updateFloatPosition() ticking = false }) ticking = true } }) ``` 这样一帧最多算一次,而不是每次 scroll 事件都算。读写分离也是老生常谈的优化手段:先把所有需要读取的几何信息读出来,再统一写样式,避免读-写-读-写交替触发多次重排。 ## 兼容性和降级 `IntersectionObserver` 在现代浏览器里支持已经很好了,Chrome 51+、Firefox 55+、Safari 12.1+ 都没问题。真正需要留意的是旧版安卓 WebView 和一些嵌入式浏览器。如果项目里有这类环境,可以做个特性检测降级: ```js if ('IntersectionObserver' in window) { // 使用 observer } else { // 退回 scroll + 节流 + getBoundingClientRect } ``` 降级方案里记得给 scroll 加节流,并且尽量缩小遍历范围,比如只检查尚未加载的元素,而不是每次全量遍历。 另外 `IntersectionObserver` 的回调是在主线程执行的,如果回调里做了很重的同步操作,一样会卡。所以回调里尽量只做状态标记,把图片加载、数据上报这类事情丢给异步任务或空闲回调。 那次事故之后我们把项目里所有滚动相关的可见性判断都梳理了一遍,能换 observer 的换掉,必须用几何 API 的加上 rAF 和读写分离。页面滚动明显顺滑了,长列表在低端机上的帧率也稳定下来。工具本身不复杂,理解它背后的渲染机制,用对地方就行。 推荐查看天气:http://www.362tq.com/rongshui/zuotian.html 推荐查看天气:http://www.362tq.com/rongshui/7tian.html 推荐查看天气:http://www.362tq.com/rongshui/10tian.html 推荐查看天气:http://www.362tq.com/rongshui/15tian.html 推荐查看天气:http://www.362tq.com/rongshui/40tian.html 推荐查看天气:http://www.362tq.com/sanjiang/ 推荐查看天气:http://www.362tq.com/sanjiang/zuotian.html 推荐查看天气:http://www.362tq.com/sanjiang/7tian.html 推荐查看天气:http://www.362tq.com/sanjiang/10tian.html 推荐查看天气:http://www.362tq.com/sanjiang/15tian.html 推荐查看天气:http://www.362tq.com/sanjiang/40tian.html 推荐查看天气:http://www.362tq.com/guilin/ 推荐查看天气:http://www.362tq.com/guilin/zuotian.html 推荐查看天气:http://www.362tq.com/guilin/7tian.html 推荐查看天气:http://www.362tq.com/guilin/10tian.html 推荐查看天气:http://www.362tq.com/guilin/15tian.html 推荐查看天气:http://www.362tq.com/guilin/40tian.html 推荐查看天气:http://www.362tq.com/xiufeng/ 推荐查看天气:http://www.362tq.com/xiufeng/zuotian.html 推荐查看天气:http://www.362tq.com/xiufeng/7tian.html 推荐查看天气:http://www.362tq.com/xiufeng/10tian.html 推荐查看天气:http://www.362tq.com/xiufeng/15tian.html 推荐查看天气:http://www.362tq.com/xiufeng/40tian.html 推荐查看天气:http://www.362tq.com/diecai/ 推荐查看天气:http://www.362tq.com/diecai/zuotian.html 推荐查看天气:http://www.362tq.com/diecai/7tian.html 推荐查看天气:http://www.362tq.com/diecai/10tian.html 推荐查看天气:http://www.362tq.com/diecai/15tian.html 推荐查看天气:http://www.362tq.com/diecai/40tian.html 推荐查看天气:http://www.362tq.com/xiangshan/ 推荐查看天气:http://www.362tq.com/xiangshan/zuotian.html 推荐查看天气:http://www.362tq.com/xiangshan/7tian.html 推荐查看天气:http://www.362tq.com/xiangshan/10tian.html 推荐查看天气:http://www.362tq.com/xiangshan/15tian.html 推荐查看天气:http://www.362tq.com/xiangshan/40tian.html 推荐查看天气:http://www.362tq.com/qixing/ 推荐查看天气:http://www.362tq.com/qixing/zuotian.html 推荐查看天气:http://www.362tq.com/qixing/7tian.html 推荐查看天气:http://www.362tq.com/qixing/10tian.html 推荐查看天气:http://www.362tq.com/qixing/15tian.html 推荐查看天气:http://www.362tq.com/qixing/40tian.html 推荐查看天气:http://www.362tq.com/yangshuo/ 推荐查看天气:http://www.362tq.com/yangshuo/zuotian.html 推荐查看天气:http://www.362tq.com/yangshuo/7tian.html 推荐查看天气:http://www.362tq.com/yangshuo/10tian.html 推荐查看天气:http://www.362tq.com/yangshuo/15tian.html 推荐查看天气:http://www.362tq.com/yangshuo/40tian.html 推荐查看天气:http://www.362tq.com/lingui/ 推荐查看天气:http://www.362tq.com/lingui/zuotian.html 推荐查看天气:http://www.362tq.com/lingui/7tian.html 推荐查看天气:http://www.362tq.com/lingui/10tian.html 推荐查看天气:http://www.362tq.com/lingui/15tian.html 推荐查看天气:http://www.362tq.com/lingui/40tian.html 推荐查看天气:http://www.362tq.com/guilinlingchuan/ 推荐查看天气:http://www.362tq.com/guilinlingchuan/zuotian.html 推荐查看天气:http://www.362tq.com/guilinlingchuan/7tian.html 推荐查看天气:http://www.362tq.com/guilinlingchuan/10tian.html 推荐查看天气:http://www.362tq.com/guilinlingchuan/15tian.html 推荐查看天气:http://www.362tq.com/guilinlingchuan/40tian.html 推荐查看天气:http://www.362tq.com/quanzhouxian/ 推荐查看天气:http://www.362tq.com/quanzhouxian/zuotian.html 推荐查看天气:http://www.362tq.com/quanzhouxian/7tian.html 推荐查看天气:http://www.362tq.com/quanzhouxian/10tian.html 推荐查看天气:http://www.362tq.com/quanzhouxian/15tian.html 推荐查看天气:http://www.362tq.com/quanzhouxian/40tian.html 推荐查看天气:http://www.362tq.com/xinganxian/ 推荐查看天气:http://www.362tq.com/xinganxian/zuotian.html 推荐查看天气:http://www.362tq.com/xinganxian/7tian.html 推荐查看天气:http://www.362tq.com/xinganxian/10tian.html 推荐查看天气:http://www.362tq.com/xinganxian/15tian.html 推荐查看天气:http://www.362tq.com/xinganxian/40tian.html 推荐查看天气:http://www.362tq.com/yongfu/ 推荐查看天气:http://www.362tq.com/yongfu/zuotian.html 推荐查看天气:http://www.362tq.com/yongfu/7tian.html 推荐查看天气:http://www.362tq.com/yongfu/10tian.html 推荐查看天气:http://www.362tq.com/yongfu/15tian.html 推荐查看天气:http://www.362tq.com/yongfu/40tian.html 推荐查看天气:http://www.362tq.com/guanyang/ 推荐查看天气:http://www.362tq.com/guanyang/zuotian.html 推荐查看天气:http://www.362tq.com/guanyang/7tian.html 推荐查看天气:http://www.362tq.com/guanyang/10tian.html 推荐查看天气:http://www.362tq.com/guanyang/15tian.html 推荐查看天气:http://www.362tq.com/guanyang/40tian.html 推荐查看天气:http://www.362tq.com/longsheng/ 推荐查看天气:http://www.362tq.com/longsheng/zuotian.html 推荐查看天气:http://www.362tq.com/longsheng/7tian.html 推荐查看天气:http://www.362tq.com/longsheng/10tian.html 推荐查看天气:http://www.362tq.com/longsheng/15tian.html 推荐查看天气:http://www.362tq.com/longsheng/40tian.html 推荐查看天气:http://www.362tq.com/ziyuan/ 推荐查看天气:http://www.362tq.com/ziyuan/zuotian.html 推荐查看天气:http://www.362tq.com/ziyuan/7tian.html 推荐查看天气:http://www.362tq.com/ziyuan/10tian.html 推荐查看天气:http://www.362tq.com/ziyuan/15tian.html 推荐查看天气:http://www.362tq.com/ziyuan/40tian.html 推荐查看天气:http://www.362tq.com/pingle/ 推荐查看天气:http://www.362tq.com/pingle/zuotian.html 推荐查看天气:http://www.362tq.com/pingle/7tian.html 推荐查看天气:http://www.362tq.com/pingle/10tian.html 推荐查看天气:http://www.362tq.com/pingle/15tian.html 推荐查看天气:http://www.362tq.com/pingle/40tian.html 推荐查看天气:http://www.362tq.com/lipu/ 推荐查看天气:http://www.362tq.com/lipu/zuotian.html 推荐查看天气:http://www.362tq.com/lipu/7tian.html 推荐查看天气:http://www.362tq.com/lipu/10tian.html 推荐查看天气:http://www.362tq.com/lipu/15tian.html 推荐查看天气:http://www.362tq.com/lipu/40tian.html 推荐查看天气:http://www.362tq.com/gongcheng/ 推荐查看天气:http://www.362tq.com/gongcheng/zuotian.html 推荐查看天气:http://www.362tq.com/gongcheng/7tian.html 推荐查看天气:http://www.362tq.com/gongcheng/10tian.html 推荐查看天气:http://www.362tq.com/gongcheng/15tian.html 推荐查看天气:http://www.362tq.com/gongcheng/40tian.html 推荐查看天气:http://www.362tq.com/wuzhou/ 推荐查看天气:http://www.362tq.com/wuzhou/zuotian.html 推荐查看天气:http://www.362tq.com/wuzhou/7tian.html 推荐查看天气:http://www.362tq.com/wuzhou/10tian.html 推荐查看天气:http://www.362tq.com/wuzhou/15tian.html 推荐查看天气:http://www.362tq.com/wuzhou/40tian.html 推荐查看天气:http://www.362tq.com/wanxiu/ 推荐查看天气:http://www.362tq.com/wanxiu/zuotian.html 推荐查看天气:http://www.362tq.com/wanxiu/7tian.html 推荐查看天气:http://www.362tq.com/wanxiu/10tian.html 推荐查看天气:http://www.362tq.com/wanxiu/15tian.html 推荐查看天气:http://www.362tq.com/wanxiu/40tian.html 推荐查看天气:http://www.362tq.com/cangwu/ 推荐查看天气:http://www.362tq.com/cangwu/zuotian.html 推荐查看天气:http://www.362tq.com/cangwu/7tian.html 推荐查看天气:http://www.362tq.com/cangwu/10tian.html 推荐查看天气:http://www.362tq.com/cangwu/15tian.html 推荐查看天气:http://www.362tq.com/cangwu/40tian.html 推荐查看天气:http://www.362tq.com/tengxian/ 推荐查看天气:http://www.362tq.com/tengxian/zuotian.html 推荐查看天气:http://www.362tq.com/tengxian/7tian.html 推荐查看天气:http://www.362tq.com/tengxian/10tian.html 推荐查看天气:http://www.362tq.com/tengxian/15tian.html 推荐查看天气:http://www.362tq.com/tengxian/40tian.html 推荐查看天气:http://www.362tq.com/mengshan/ 推荐查看天气:http://www.362tq.com/mengshan/zuotian.html 推荐查看天气:http://www.362tq.com/mengshan/7tian.html 推荐查看天气:http://www.362tq.com/mengshan/10tian.html 推荐查看天气:http://www.362tq.com/mengshan/15tian.html 推荐查看天气:http://www.362tq.com/mengshan/40tian.html 推荐查看天气:http://www.362tq.com/cenxi/ 推荐查看天气:http://www.362tq.com/cenxi/zuotian.html 推荐查看天气:http://www.362tq.com/cenxi/7tian.html 推荐查看天气:http://www.362tq.com/cenxi/10tian.html 推荐查看天气:http://www.362tq.com/cenxi/15tian.html 推荐查看天气:http://www.362tq.com/cenxi/40tian.html 推荐查看天气:http://www.362tq.com/beihai/ 推荐查看天气:http://www.362tq.com/beihai/zuotian.html 推荐查看天气:http://www.362tq.com/beihai/7tian.html 推荐查看天气:http://www.362tq.com/beihai/10tian.html 推荐查看天气:http://www.362tq.com/beihai/15tian.html 推荐查看天气:http://www.362tq.com/beihai/40tian.html 推荐查看天气:http://www.362tq.com/yinhai/ 推荐查看天气:http://www.362tq.com/yinhai/zuotian.html 推荐查看天气:http://www.362tq.com/yinhai/7tian.html 推荐查看天气:http://www.362tq.com/yinhai/10tian.html 推荐查看天气:http://www.362tq.com/yinhai/15tian.html 推荐查看天气:http://www.362tq.com/yinhai/40tian.html 推荐查看天气:http://www.362tq.com/tieshangang/ 推荐查看天气:http://www.362tq.com/tieshangang/zuotian.html 推荐查看天气:http://www.362tq.com/tieshangang/7tian.html 推荐查看天气:http://www.362tq.com/tieshangang/10tian.html 推荐查看天气:http://www.362tq.com/tieshangang/15tian.html 推荐查看天气:http://www.362tq.com/tieshangang/40tian.html 推荐查看天气:http://www.362tq.com/hepu/ 推荐查看天气:http://www.362tq.com/hepu/zuotian.html 推荐查看天气:http://www.362tq.com/hepu/7tian.html 推荐查看天气:http://www.362tq.com/hepu/10tian.html 推荐查看天气:http://www.362tq.com/hepu/15tian.html 推荐查看天气:http://www.362tq.com/hepu/40tian.html 推荐查看天气:http://www.362tq.com/fangchenggang/ 推荐查看天气:http://www.362tq.com/fangchenggang/zuotian.html 推荐查看天气:http://www.362tq.com/fangchenggang/7tian.html 推荐查看天气:http://www.362tq.com/fangchenggang/10tian.html 推荐查看天气:http://www.362tq.com/fangchenggang/15tian.html 推荐查看天气:http://www.362tq.com/fangchenggang/40tian.html 推荐查看天气:http://www.362tq.com/gangkou/ 推荐查看天气:http://www.362tq.com/gangkou/zuotian.html 推荐查看天气:http://www.362tq.com/gangkou/7tian.html 推荐查看天气:http://www.362tq.com/gangkou/10tian.html 推荐查看天气:http://www.362tq.com/gangkou/15tian.html 推荐查看天气:http://www.362tq.com/gangkou/40tian.html 推荐查看天气:http://www.362tq.com/fangcheng/ 推荐查看天气:http://www.362tq.com/fangcheng/zuotian.html 推荐查看天气:http://www.362tq.com/fangcheng/7tian.html 推荐查看天气:http://www.362tq.com/fangcheng/10tian.html 推荐查看天气:http://www.362tq.com/fangcheng/15tian.html 推荐查看天气:http://www.362tq.com/fangcheng/40tian.html 推荐查看天气:http://www.362tq.com/shangsi/ 推荐查看天气:http://www.362tq.com/shangsi/zuotian.html 推荐查看天气:http://www.362tq.com/shangsi/7tian.html 推荐查看天气:http://www.362tq.com/shangsi/10tian.html 推荐查看天气:http://www.362tq.com/shangsi/15tian.html 推荐查看天气:http://www.362tq.com/shangsi/40tian.html 推荐查看天气:http://www.362tq.com/dongxingshi/ 推荐查看天气:http://www.362tq.com/dongxingshi/zuotian.html 推荐查看天气:http://www.362tq.com/dongxingshi/7tian.html