Skip to content

Commit b868e15

Browse files
committed
wip: benchmark manual read
1 parent 924431d commit b868e15

File tree

3 files changed

+29
-80
lines changed

3 files changed

+29
-80
lines changed
Lines changed: 19 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
import { bench } from 'vitest'
2-
import {
3-
computed,
4-
effect,
5-
reactive,
6-
readonly,
7-
shallowRef,
8-
triggerRef,
9-
} from '../src'
2+
import { effect, reactive, reactiveReadArray, shallowReadArray } from '../src'
103

114
for (let amount = 1e1; amount < 1e4; amount *= 10) {
125
{
@@ -16,7 +9,7 @@ for (let amount = 1e1; amount < 1e4; amount *= 10) {
169
}
1710
const arr = reactive(rawArray)
1811

19-
bench(`track for loop on reactive array, ${amount} elements`, () => {
12+
bench(`track for loop, ${amount} elements`, () => {
2013
let sum = 0
2114
effect(() => {
2215
for (let i = 0; i < arr.length; i++) {
@@ -33,11 +26,12 @@ for (let amount = 1e1; amount < 1e4; amount *= 10) {
3326
}
3427
const arr = reactive(rawArray)
3528

36-
bench(`track iteration on reactive array, ${amount} elements`, () => {
29+
bench(`track manual reactiveReadArray, ${amount} elements`, () => {
3730
let sum = 0
3831
effect(() => {
39-
for (let x of arr) {
40-
sum += x
32+
const raw = shallowReadArray(arr)
33+
for (let i = 0; i < raw.length; i++) {
34+
sum += raw[i]
4135
}
4236
})
4337
})
@@ -50,54 +44,7 @@ for (let amount = 1e1; amount < 1e4; amount *= 10) {
5044
}
5145
const arr = reactive(rawArray)
5246

53-
bench(`track forEach on reactive array, ${amount} elements`, () => {
54-
let sum = 0
55-
effect(() => {
56-
arr.forEach(x => (sum += x))
57-
})
58-
})
59-
}
60-
61-
{
62-
const rawArray: number[] = []
63-
for (let i = 0, n = amount; i < n; i++) {
64-
rawArray.push(i)
65-
}
66-
const arr = reactive(rawArray)
67-
68-
bench(`track reduce on reactive array, ${amount} elements`, () => {
69-
let sum = 0
70-
effect(() => {
71-
sum = arr.reduce((v, a) => a + v, 0)
72-
})
73-
})
74-
}
75-
76-
{
77-
const rawArray: number[] = []
78-
for (let i = 0, n = amount; i < n; i++) {
79-
rawArray.push(i)
80-
}
81-
const arr = readonly(rawArray)
82-
83-
bench(`track for loop on readonly array, ${amount} elements`, () => {
84-
let sum = 0
85-
effect(() => {
86-
for (let i = 0; i < arr.length; i++) {
87-
sum += arr[i]
88-
}
89-
})
90-
})
91-
}
92-
93-
{
94-
const rawArray: number[] = []
95-
for (let i = 0, n = amount; i < n; i++) {
96-
rawArray.push(i)
97-
}
98-
const arr = readonly(rawArray)
99-
100-
bench(`track iteration on readonly array, ${amount} elements`, () => {
47+
bench(`track iteration, ${amount} elements`, () => {
10148
let sum = 0
10249
effect(() => {
10350
for (let x of arr) {
@@ -112,9 +59,9 @@ for (let amount = 1e1; amount < 1e4; amount *= 10) {
11259
for (let i = 0, n = amount; i < n; i++) {
11360
rawArray.push(i)
11461
}
115-
const arr = readonly(rawArray)
62+
const arr = reactive(rawArray)
11663

117-
bench(`track forEach on readonly array, ${amount} elements`, () => {
64+
bench(`track forEach, ${amount} elements`, () => {
11865
let sum = 0
11966
effect(() => {
12067
arr.forEach(x => (sum += x))
@@ -127,9 +74,9 @@ for (let amount = 1e1; amount < 1e4; amount *= 10) {
12774
for (let i = 0, n = amount; i < n; i++) {
12875
rawArray.push(i)
12976
}
130-
const arr = readonly(rawArray)
77+
const arr = reactive(rawArray)
13178

132-
bench(`track reduce on readonly array, ${amount} elements`, () => {
79+
bench(`track reduce, ${amount} elements`, () => {
13380
let sum = 0
13481
effect(() => {
13582
sum = arr.reduce((v, a) => a + v, 0)
@@ -146,7 +93,7 @@ for (let amount = 1e1; amount < 1e4; amount *= 10) {
14693
effect(() => r.reduce((v, a) => a + v, 0))
14794

14895
bench(
149-
`trigger index mutation (1st only) on *reactive* array (tracked with reduce), ${amount} elements`,
96+
`trigger index mutation (1st only), tracked with reduce, ${amount} elements`,
15097
() => {
15198
r[0]++
15299
},
@@ -162,7 +109,7 @@ for (let amount = 1e1; amount < 1e4; amount *= 10) {
162109
effect(() => r.reduce((v, a) => a + v, 0))
163110

164111
bench(
165-
`trigger index mutation (all) on *reactive* array (tracked with reduce), ${amount} elements`,
112+
`trigger index mutation (all), tracked with reduce, ${amount} elements`,
166113
() => {
167114
for (let i = 0, n = r.length; i < n; i++) {
168115
r[i]++
@@ -184,12 +131,9 @@ for (let amount = 1e1; amount < 1e4; amount *= 10) {
184131
}
185132
})
186133

187-
bench(
188-
`push() trigger on reactive array tracked via iteration, ${amount} elements`,
189-
() => {
190-
arr.push(1)
191-
},
192-
)
134+
bench(`push() trigger, tracked via iteration, ${amount} elements`, () => {
135+
arr.push(1)
136+
})
193137
}
194138

195139
{
@@ -203,11 +147,8 @@ for (let amount = 1e1; amount < 1e4; amount *= 10) {
203147
arr.forEach(x => (sum += x))
204148
})
205149

206-
bench(
207-
`push() trigger on reactive array tracked via forEach, ${amount} elements`,
208-
() => {
209-
arr.push(1)
210-
},
211-
)
150+
bench(`push() trigger, tracked via forEach, ${amount} elements`, () => {
151+
arr.push(1)
152+
})
212153
}
213154
}

packages/reactivity/src/arrayInstrumentations.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@ import { endBatch, pauseTracking, resetTracking, startBatch } from './effect'
33
import { isProxy, isShallow, toRaw, toReactive } from './reactive'
44
import { ARRAY_ITERATE_KEY, track } from './dep'
55

6+
/**
7+
* Track array iteration and return:
8+
* - if input is reactive: a cloned raw array with reactive values
9+
* - if input is non-reactive or shallowReactive: the original raw array
10+
*/
611
export function reactiveReadArray<T>(array: T[]): T[] {
712
const raw = toRaw(array)
813
if (raw === array) return raw
914
track(raw, TrackOpTypes.ITERATE, ARRAY_ITERATE_KEY)
1015
return isShallow(array) ? raw : raw.map(toReactive)
1116
}
1217

13-
function shallowReadArray<T>(arr: T[]): T[] {
18+
/**
19+
* Track array iteration and return raw array
20+
*/
21+
export function shallowReadArray<T>(arr: T[]): T[] {
1422
track((arr = toRaw(arr)), TrackOpTypes.ITERATE, ARRAY_ITERATE_KEY)
1523
return arr
1624
}

packages/reactivity/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,5 @@ export {
7373
getCurrentScope,
7474
onScopeDispose,
7575
} from './effectScope'
76-
export { reactiveReadArray } from './arrayInstrumentations'
76+
export { reactiveReadArray, shallowReadArray } from './arrayInstrumentations'
7777
export { TrackOpTypes, TriggerOpTypes, ReactiveFlags } from './constants'

0 commit comments

Comments
 (0)