angular-tree-component
angular-tree-component copied to clipboard
getNodeById is very slow, especially in large loops
We need a API for getNodeById with O(1) time complexity. But in current implemenation, the time complexity is very high. In the large loops, the performance is very bad.
I have a tree with about 3000 nodes, loop each node cost me about 1.5 seconds and I can see significant lag.
` getNodeBy(predicate, startNode = null) {
startNode = startNode || this.virtualRoot;
if (!startNode.children) return null;
const found = find(startNode.children, predicate);
if (found) { // found in children
return found;
} else { // look in children's children
for (let child of startNode.children) {
const foundInChildren = this.getNodeBy(predicate, child);
if (foundInChildren) return foundInChildren;
}
}
}`
I have to use a node container to get node from id, here is my code:
`private flattenTreeNodeList(node: TreeNode) { if (node) { this.treeNodeContainer.add(node); if (node.children) { for (let child of node.children) { this.flattenTreeNodeList(child); } } } }
export class TreeNodeContainer { public readonly nodes: { [nodeId: string]: TreeNode } = {};
get size(): number { return Object.keys(this.nodes).length; }
get values(): TreeNode[] {
const values = Object.keys(this.nodes).map(key => this.nodes[key]);
return values;
}
constructor() { }
add(node: TreeNode): void {
const nodeId = node.id;
this.nodes[nodeId] = node;
}
get(nodeId: string): TreeNode | undefined {
return this.nodes[nodeId];
}
contains(nodeId: string): boolean {
return this.nodes.hasOwnProperty(nodeId);
}
remove(nodeId: string): void {
delete (this.nodes[nodeId]);
}
}`
Hi, I recently opened a slack channel for the library's users community. I think it will be a good place for the community to discuss different ways of implementation, to ask for feature requests, collaborate on PRs, and hear about bugfixes and releases.
I invite you to join: https://angular-tree-component.herokuapp.com/
Hi, is there a solution for this?