Skip to content

Commit 20e8078

Browse files
committed
optimize internal active children check
1 parent 7bb0e24 commit 20e8078

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

.changeset/bright-jeans-draw.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@effect-atom/atom": patch
3+
"@effect-atom/atom-livestore": patch
4+
"@effect-atom/atom-react": patch
5+
"@effect-atom/atom-vue": patch
6+
---
7+
8+
optimize internal active children check

packages/atom/src/internal/registry.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -477,16 +477,23 @@ function childrenAreActive(children: Array<Node<any>>): boolean {
477477
if (children.length === 0) {
478478
return false
479479
}
480-
for (let i = 0, len = children.length; i < len; i++) {
481-
const child = children[i]
482-
if (!child.atom.lazy || child.listeners.length > 0) {
483-
return true
484-
}
485-
}
486-
for (let i = 0, len = children.length; i < len; i++) {
487-
if (childrenAreActive(children[i].children)) {
488-
return true
480+
let current: Array<Node<any>> | undefined = children
481+
let stack: Array<Array<Node<any>>> | undefined
482+
let stackIndex = 0
483+
while (current !== undefined) {
484+
for (let i = 0, len = current.length; i < len; i++) {
485+
const child = current[i]
486+
if (!child.atom.lazy || child.listeners.length > 0) {
487+
return true
488+
} else if (child.children.length > 0) {
489+
if (stack === undefined) {
490+
stack = [child.children]
491+
} else {
492+
stack.push(child.children)
493+
}
494+
}
489495
}
496+
current = stack?.[stackIndex++]
490497
}
491498
return false
492499
}

0 commit comments

Comments
 (0)