⚡
some of us get dipped in flat,
⚡
some in satin,
⚡
some in gloss,
⚡
but every once in a while you find someone who's iridescent,
⚡
and when you do,nothing will ever compare.
This project forked from wilsonpage/fastdom
Eliminates layout thrashing by batching DOM measurement and mutation tasks
在真正看源码之前, 首先通过其fastdom的api使用以及对于会导致布局抖动的原因, 我们将自己尝试着去解决这个问题, fastdom的使用很简单, 就两个方法: measure(读相关), mutate(写相关)
fastdom.measure(function() {
var top = ball.offsetTop;
fastdom.mutate(function() {
ball.style.left = ((Math.sin(ball.offsetTop + timestamp/1000) + 1) * 500) + 'px'
});
});
对于其使用, 我们可以大致知道measure将是记录读取相关的操作, mutate是记录写相关的操作, 所以:
var FastDom = function() {
this.reads = [] // 记录读操作的队列
this.writes = [] // 记录写操作的队列
}
Fastdom.prototype = {
measure: function(func) {
this.reads.push(func) // 读操作入队
},
mutate: function(func) {
this.writes.push(func) // 写操作入队
},
}
好了, 我们将操作入队之后呢, 我们可以看到fastdom的api, 调用完measure, mutate 之后就不需要再另外调用其他api 就可以开始执行任务了, 那所以measure/mutate肯定就会开始触发任务队列的执行.
var FastDom = function() {
this.reads = [] // 记录读操作的队列
this.writes = [] // 记录写操作的队列
}
Fastdom.prototype = {
measure: function(func) {
this.reads.push(func) // 读操作入队
this.schedule() // 调度
},
mutate: function(func) {
this.writes.push(func) // 写操作入队
this.schedule() // 调度
},
schedule() { }
}
那么如何调度呢, 在解决怎么调度之前, 我们任务队列有了, 那怎么执行这个任务队列呢?
Fastdom.prototype = {
...,
runTasks: function(tasks) {
var task; while (task = tasks.shift()) task();
},
}
上面这个方法应该就不用多说了吧, 那接下来就是对于我们的读写分离, 怎么分离呢, 就是先执行完读队列中的内容, 再执行写队列中的内容:
Fastdom.prototype = {
...,
flushTasks() {
this.runTasks(this.reads)
this.runTasks(this.writes)
},
}
那么接下来就是调度的问题了, 我们知道, 对于每个measure, mutate我们都触发了调度, 那么对于批量的读写, 我们首先需要确保的就是只执行一次调度操作, 当全部读写操作执行完, 我们再进行下一轮, 所以这边我们在调度中首先需要的就是引入锁, 还有我们每一次调度其中就是执行完所有的读写操作, 那么我们可以直接就调用flushTasks吗
Fastdom.prototype = {
flushTasks() {
this.runTasks(this.reads)
this.runTasks(this.writes)
this.lock = false // 解锁
},
schedule() {
if (this.lock) return
this.lock = true // 锁上
this.flushTasks() // 执行读写操作, <<<思考>>>
}
}
好了, 大家觉得上述代码有问题吗(那不是废话吗, 我都没说结束了), 好的, 我们知道, 我们需要做的是, 收集批量(重点)的读写, 但是我们上面的代码中, 我们可以想我们for循环400次, 执行了400次的measure, measure又触发了schedule, 而schedule马上执行flushTasks, 我们知道前端是单线程, 所以如上代码其实就是在同步执行, 每次只收集到一次读/写操作, 就触发flushTasks了, 所以我们希望的是收集较多的reads, 再去执行flushTasks, 而我们又是在做dom的更新, 因此将flushTasks的执行放到requestAnimationFrame就在合适不过了, so:
Fastdom.prototype = {
flushTasks() {
this.runTasks(this.reads)
this.runTasks(this.writes)
this.lock = false // 解锁
},
schedule() {
if (this.lock) return
this.lock = true // 锁上
window.requestAnimationFrame(this.flushTasks.bind(this)) // RAF
}
}
这时候再使用我们实现的fastdom, 这时候我们可以发现performance:
没有了大量的强制布局, 一帧时间也降到了 30ms, 好了, 经此, 大家再去看fastdom的源码, 应该没什么问题了, 感谢各位老板
首先让我们先了解一下以下内容
JavaScript -> Style -> Layout -> Paint -> Composite
(主要用来触发视觉变换) (对样式重新计算) (布局, 大小/位置) (绘制) (复合, 不是所有东西都画在一个图层上, 类似ps)
理论上这五个步骤都是必须经历的, 但有些样式不会影响 Layout/Paint, 所有浏览器进行了优化, 这些样式可以不经历 Layout/Paint
当出现如下代码:
const update = () => {
dom.style.width = dom.offsetTop + Math.random() * 500 + 'px'
window.requestAnimationFrame(update)
}
浏览器为了尽量的提高布局性能, 会尽量的去把对修改布局属性的一些操作推迟, 但当你为了去获取一些获得布局相关的属性, 比如offsetTop, 浏览器不得不立即去计算获取最新的值, 所以在这边赋值前会强制的去计算, 会产生连续的强制回流, 出现卡顿
解决:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.