mirror of
https://github.com/EnixCoda/Gitako.git
synced 2026-03-11 08:54:44 +00:00
perf(nodes-generator): 4x faster node generating
refactored recurse to loop, time for processing 4000 items reduces from 6ms to 1.4ms
This commit is contained in:
parent
42db8a8eec
commit
5b1a7c6b89
1 changed files with 18 additions and 11 deletions
|
|
@ -171,19 +171,26 @@ export default class VisibleNodesGenerator {
|
|||
this.focusedNode = null
|
||||
this.depths.clear()
|
||||
const nodesSet = new Set() // prevent duplication
|
||||
const get = (nodes, depth = 0) => {
|
||||
return [].concat(
|
||||
...nodes.map(node => {
|
||||
if (nodesSet.has(node)) return []
|
||||
this.depths.set(node, depth)
|
||||
nodesSet.add(node)
|
||||
const children = this.expandedNodes.has(node) ? get(node.contents, depth + 1) : []
|
||||
return [node, ...children]
|
||||
})
|
||||
)
|
||||
const nodes = [], stack = this.searchedNodes.slice().reverse()
|
||||
let current, depth = 0
|
||||
while (stack.length) {
|
||||
current = stack.pop()
|
||||
if (current === null) {
|
||||
depth -= 1
|
||||
continue
|
||||
}
|
||||
if (nodesSet.has(current)) continue
|
||||
nodes.push(current)
|
||||
nodesSet.add(current)
|
||||
this.depths.set(current, depth)
|
||||
if (this.expandedNodes.has(current)) {
|
||||
stack.push(null) // use null as pop depth flag
|
||||
stack.push(...current.contents.slice().reverse())
|
||||
depth += 1
|
||||
}
|
||||
}
|
||||
this.visibleNodes = {
|
||||
nodes: get(this.searchedNodes),
|
||||
nodes,
|
||||
depths: this.depths,
|
||||
expandedNodes: this.expandedNodes,
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue