Skip to content

Commit

Permalink
perf: avoid array map in v-for read array
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 26, 2024
1 parent b868e15 commit 13d31d5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/reactivity/__benchmarks__/reactiveArray.bench.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { bench } from 'vitest'
import { effect, reactive, reactiveReadArray, shallowReadArray } from '../src'
import { effect, reactive, shallowReadArray } from '../src'

for (let amount = 1e1; amount < 1e4; amount *= 10) {
{
Expand Down
2 changes: 2 additions & 0 deletions packages/reactivity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export {
shallowReadonly,
markRaw,
toRaw,
toReactive,
toReadonly,
type Raw,
type DeepReadonly,
type ShallowReactive,
Expand Down
14 changes: 10 additions & 4 deletions packages/runtime-core/src/helpers/renderList.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { VNode, VNodeChild } from '../vnode'
import { reactiveReadArray } from '@vue/reactivity'
import { isReactive, shallowReadArray, toReactive } from '@vue/reactivity'
import { isArray, isObject, isString } from '@vue/shared'
import { warn } from '../warning'

Expand Down Expand Up @@ -60,14 +60,20 @@ export function renderList(
let ret: VNodeChild[]
const cached = (cache && cache[index!]) as VNode[] | undefined
const sourceIsArray = isArray(source)
const sourceIsReactiveArray = sourceIsArray && isReactive(source)

if (sourceIsArray || isString(source)) {
if (sourceIsArray) {
source = reactiveReadArray(source)
if (sourceIsReactiveArray) {
source = shallowReadArray(source)
}
ret = new Array(source.length)
for (let i = 0, l = source.length; i < l; i++) {
ret[i] = renderItem(source[i], i, undefined, cached && cached[i])
ret[i] = renderItem(
sourceIsReactiveArray ? toReactive(source[i]) : source[i],
i,
undefined,
cached && cached[i],
)
}
} else if (typeof source === 'number') {
if (__DEV__ && !Number.isInteger(source)) {
Expand Down

0 comments on commit 13d31d5

Please sign in to comment.