Skip to content

Commit

Permalink
perf(schema): improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Sep 17, 2021
1 parent 0b81fa9 commit 184884c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
10 changes: 6 additions & 4 deletions packages/json-schema/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,20 @@ export const compile = <Source = any, Scope = any>(
source: Source,
scope?: Scope
): any => {
const seenObjects = new WeakSet()
const seenObjects = []
const compile = (source: any) => {
if (isStr(source)) {
return shallowCompile(source, scope)
} else if (isArr(source)) {
return source.map((value: any) => compile(value))
} else if (isPlainObj(source)) {
if (isNoNeedCompileObject(source)) return source
if (seenObjects.has(source)) {
const seenIndex = seenObjects.indexOf(source)
if (seenIndex > -1) {
return source
}
seenObjects.add(source)
const addIndex = seenObjects.length
seenObjects.push(source)
const results = reduce(
source,
(buf, value, key) => {
Expand All @@ -82,7 +84,7 @@ export const compile = <Source = any, Scope = any>(
},
{}
)
seenObjects.delete(source)
seenObjects.splice(addIndex, 1)
return results
}
return source
Expand Down
10 changes: 6 additions & 4 deletions packages/json-schema/src/traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@ export const traverse = (
visitor: (value: any, path: Array<string | number>, address: string) => void,
filter?: (value: any, path: Array<string | number>) => boolean
) => {
const seenObjects = new WeakSet()
const seenObjects = []
const root = target
const traverse = (target: any, path = [], address = '') => {
if (filter?.(target, path) === false) return

if (isPlainObj(target)) {
if (seenObjects.has(target)) {
const seenIndex = seenObjects.indexOf(target)
if (seenIndex > -1) {
return
}
seenObjects.add(target)
const addIndex = seenObjects.length
seenObjects.push(target)
if (isNoNeedCompileObject(target) && root !== target) {
visitor(target, path, address)
return
}
each(target, (value, key) => {
traverse(value, path.concat(key), address + '.' + key)
})
seenObjects.delete(target)
seenObjects.splice(addIndex, 1)
} else {
visitor(target, path, address)
}
Expand Down
7 changes: 7 additions & 0 deletions packages/reactive/src/__tests__/observable.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { observable } from '../'

test('array mutation', () => {
const arr = observable([1, 2, 3, 4])
arr.splice(2, 1)
expect(arr).toEqual([1, 2, 4])
})
4 changes: 2 additions & 2 deletions packages/reactive/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,14 @@ export const baseHandlers: ProxyHandler<any> = {
return true
},
deleteProperty(target, key) {
const res = Reflect.deleteProperty(target, key)
const oldValue = target[key]
delete target[key]
runReactionsFromTargetKey({
target,
key,
oldValue,
type: 'delete',
})
return res
return true
},
}

0 comments on commit 184884c

Please sign in to comment.