Skip to content

Commit

Permalink
fix: Reduce use of spread operator
Browse files Browse the repository at this point in the history
Per @Qrtn's suggestion in:
#73 (comment)

Also makes the large array test use a
more realisitic result shape, with objects
with various contents.
  • Loading branch information
franky47 committed Aug 8, 2023
1 parent 2614809 commit d3e0b97
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/traverseTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,15 @@ describe('traverseTree', () => {
])
})

const leaf = (index: number) => ({
hello: 'world',
index
})
const array = Array.from({ length: 1_000_000 }, (_, i) => leaf(i))

test('root node is an array with 1 million rows', () => {
const visitor = jest.fn().mockImplementation(state => state)
traverseTree(Array.from(Array(1000000).keys()), visitor, null)
expect(visitor).toHaveBeenCalledTimes(1000001)
traverseTree(array, visitor, null)
expect(visitor).toHaveBeenCalledTimes(3000001)
})
})
6 changes: 4 additions & 2 deletions src/traverseTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function traverseTree<State>(
state: State
}

let stack: StackItem[] = [
const stack: StackItem[] = [
{
path: [],
type: typeOf(input),
Expand All @@ -56,7 +56,9 @@ export function traverseTree<State>(
state: newState
})
)
stack = [...stack, ...children.reverse()]
for (let i = children.length - 1; i >= 0; --i) {
stack.push(children[i])
}
}
}

Expand Down

0 comments on commit d3e0b97

Please sign in to comment.