perf(reactivity): improve reactive effect memory usage (#4001)

Based on #2345 , but with smaller API change

- Use class implementation for `ReactiveEffect`
- Switch internal creation of effects to use the class constructor
- Avoid options object allocation
- Avoid creating bound effect runner function (used in schedulers) when not necessary.
- Consumes ~17% less memory compared to last commit
- Introduces a very minor breaking change: the `scheduler` option passed to `effect` no longer receives the runner function.
This commit is contained in:
Evan You
2021-06-24 17:44:32 -04:00
parent 63a51ffcab
commit 87f69fd0bb
12 changed files with 221 additions and 208 deletions

View File

@@ -1,4 +1,3 @@
import { effect, stop } from '@vue/reactivity'
import {
queueJob,
nextTick,
@@ -576,20 +575,19 @@ describe('scheduler', () => {
// simulate parent component that toggles child
const job1 = () => {
stop(job2)
// @ts-ignore
job2.active = false
}
job1.id = 0 // need the id to ensure job1 is sorted before job2
// simulate child that's triggered by the same reactive change that
// triggers its toggle
const job2 = effect(() => spy())
expect(spy).toHaveBeenCalledTimes(1)
const job2 = () => spy()
expect(spy).toHaveBeenCalledTimes(0)
queueJob(job1)
queueJob(job2)
await nextTick()
// should not be called again
expect(spy).toHaveBeenCalledTimes(1)
// should not be called
expect(spy).toHaveBeenCalledTimes(0)
})
})