-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: add continuous reactivity benchmark (#9638)
- Loading branch information
Showing
10 changed files
with
657 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { describe, bench } from 'vitest' | ||
import { ComputedRef, Ref, computed, ref } from '../src/index' | ||
|
||
describe('computed', () => { | ||
bench('create computed', () => { | ||
computed(() => 100) | ||
}) | ||
|
||
{ | ||
let i = 0 | ||
const o = ref(100) | ||
bench('write independent ref dep', () => { | ||
o.value = i++ | ||
}) | ||
} | ||
|
||
{ | ||
const v = ref(100) | ||
computed(() => v.value * 2) | ||
let i = 0 | ||
bench("write ref, don't read computed (never invoked)", () => { | ||
v.value = i++ | ||
}) | ||
} | ||
|
||
{ | ||
const v = ref(100) | ||
computed(() => { | ||
return v.value * 2 | ||
}) | ||
let i = 0 | ||
bench("write ref, don't read computed (never invoked)", () => { | ||
v.value = i++ | ||
}) | ||
} | ||
|
||
{ | ||
const v = ref(100) | ||
const c = computed(() => { | ||
return v.value * 2 | ||
}) | ||
c.value | ||
let i = 0 | ||
bench("write ref, don't read computed (invoked)", () => { | ||
v.value = i++ | ||
}) | ||
} | ||
|
||
{ | ||
const v = ref(100) | ||
const c = computed(() => { | ||
return v.value * 2 | ||
}) | ||
let i = 0 | ||
bench('write ref, read computed', () => { | ||
v.value = i++ | ||
c.value | ||
}) | ||
} | ||
|
||
{ | ||
const v = ref(100) | ||
const computeds = [] | ||
for (let i = 0, n = 1000; i < n; i++) { | ||
const c = computed(() => { | ||
return v.value * 2 | ||
}) | ||
computeds.push(c) | ||
} | ||
let i = 0 | ||
bench("write ref, don't read 1000 computeds (never invoked)", () => { | ||
v.value = i++ | ||
}) | ||
} | ||
|
||
{ | ||
const v = ref(100) | ||
const computeds = [] | ||
for (let i = 0, n = 1000; i < n; i++) { | ||
const c = computed(() => { | ||
return v.value * 2 | ||
}) | ||
c.value | ||
computeds.push(c) | ||
} | ||
let i = 0 | ||
bench("write ref, don't read 1000 computeds (invoked)", () => { | ||
v.value = i++ | ||
}) | ||
} | ||
|
||
{ | ||
const v = ref(100) | ||
const computeds: ComputedRef<number>[] = [] | ||
for (let i = 0, n = 1000; i < n; i++) { | ||
const c = computed(() => { | ||
return v.value * 2 | ||
}) | ||
c.value | ||
computeds.push(c) | ||
} | ||
let i = 0 | ||
bench('write ref, read 1000 computeds', () => { | ||
v.value = i++ | ||
computeds.forEach(c => c.value) | ||
}) | ||
} | ||
|
||
{ | ||
const refs: Ref<number>[] = [] | ||
for (let i = 0, n = 1000; i < n; i++) { | ||
refs.push(ref(i)) | ||
} | ||
const c = computed(() => { | ||
let total = 0 | ||
refs.forEach(ref => (total += ref.value)) | ||
return total | ||
}) | ||
let i = 0 | ||
const n = refs.length | ||
bench('1000 refs, 1 computed', () => { | ||
refs[i++ % n].value++ | ||
c.value | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { bench } from 'vitest' | ||
import { computed, reactive, readonly, shallowRef, triggerRef } from '../src' | ||
|
||
for (let amount = 1e1; amount < 1e4; amount *= 10) { | ||
{ | ||
const rawArray = [] | ||
for (let i = 0, n = amount; i < n; i++) { | ||
rawArray.push(i) | ||
} | ||
const r = reactive(rawArray) | ||
const c = computed(() => { | ||
return r.reduce((v, a) => a + v, 0) | ||
}) | ||
|
||
bench(`reduce *reactive* array, ${amount} elements`, () => { | ||
for (let i = 0, n = r.length; i < n; i++) { | ||
r[i]++ | ||
} | ||
c.value | ||
}) | ||
} | ||
|
||
{ | ||
const rawArray = [] | ||
for (let i = 0, n = amount; i < n; i++) { | ||
rawArray.push(i) | ||
} | ||
const r = reactive(rawArray) | ||
const c = computed(() => { | ||
return r.reduce((v, a) => a + v, 0) | ||
}) | ||
|
||
bench( | ||
`reduce *reactive* array, ${amount} elements, only change first value`, | ||
() => { | ||
r[0]++ | ||
c.value | ||
} | ||
) | ||
} | ||
|
||
{ | ||
const rawArray = [] | ||
for (let i = 0, n = amount; i < n; i++) { | ||
rawArray.push(i) | ||
} | ||
const r = reactive({ arr: readonly(rawArray) }) | ||
const c = computed(() => { | ||
return r.arr.reduce((v, a) => a + v, 0) | ||
}) | ||
|
||
bench(`reduce *readonly* array, ${amount} elements`, () => { | ||
r.arr = r.arr.map(v => v + 1) | ||
c.value | ||
}) | ||
} | ||
|
||
{ | ||
const rawArray = [] | ||
for (let i = 0, n = amount; i < n; i++) { | ||
rawArray.push(i) | ||
} | ||
const r = shallowRef(rawArray) | ||
const c = computed(() => { | ||
return r.value.reduce((v, a) => a + v, 0) | ||
}) | ||
|
||
bench(`reduce *raw* array, copied, ${amount} elements`, () => { | ||
r.value = r.value.map(v => v + 1) | ||
c.value | ||
}) | ||
} | ||
|
||
{ | ||
const rawArray: number[] = [] | ||
for (let i = 0, n = amount; i < n; i++) { | ||
rawArray.push(i) | ||
} | ||
const r = shallowRef(rawArray) | ||
const c = computed(() => { | ||
return r.value.reduce((v, a) => a + v, 0) | ||
}) | ||
|
||
bench(`reduce *raw* array, manually triggered, ${amount} elements`, () => { | ||
for (let i = 0, n = rawArray.length; i < n; i++) { | ||
rawArray[i]++ | ||
} | ||
triggerRef(r) | ||
c.value | ||
}) | ||
} | ||
} |
Oops, something went wrong.