Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Make index of Node readonly to prevent potential perf problem #1777

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions src/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export abstract class Container<
this.getChildren().forEach((child) => {
// reset parent to prevent many _setChildrenIndices calls
child.parent = null;
child.index = 0;
child.remove();
});
this.children = [];
Expand All @@ -100,7 +99,6 @@ export abstract class Container<
this.getChildren().forEach((child) => {
// reset parent to prevent many _setChildrenIndices calls
child.parent = null;
child.index = 0;
child.destroy();
});
this.children = [];
Expand Down Expand Up @@ -139,7 +137,6 @@ export abstract class Container<
return this;
}
this._validateAdd(child);
child.index = this.getChildren().length;
child.parent = this;
child._clearCaches();
this.getChildren().push(child);
Expand Down Expand Up @@ -336,12 +333,6 @@ export abstract class Container<
node._clearSelfAndDescendantCache(attr);
});
}
_setChildrenIndices() {
this.children?.forEach(function (child, n) {
child.index = n;
});
this._requestDraw();
}
drawScene(can?: SceneCanvas, top?: Node, bufferCanvas?: SceneCanvas) {
var layer = this.getLayer()!,
canvas = can || (layer && layer.getCanvas()),
Expand Down
18 changes: 11 additions & 7 deletions src/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
[index: string]: Array<{ name: string; handler: Function }>;
} = {};
attrs: any = {};
index = 0;
_allEventListeners: null | Array<Function> = null;
parent: Container | null = null;
_cache: Map<string, any> = new Map<string, any>();
Expand All @@ -172,6 +171,17 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
// all change event listeners are attached to the prototype
}

get index() {
if (this.parent) {
return this.parent.children.indexOf(this);
}
return 0;
}

set index(_val: number) {

}

hasChildren() {
return false;
}
Expand Down Expand Up @@ -854,7 +864,6 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {

if (parent && parent.children) {
parent.children.splice(this.index, 1);
parent._setChildrenIndices();
this.parent = null;
}
}
Expand Down Expand Up @@ -1361,7 +1370,6 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
if (index < len - 1) {
this.parent.children.splice(index, 1);
this.parent.children.push(this);
this.parent._setChildrenIndices();
return true;
}
return false;
Expand All @@ -1382,7 +1390,6 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
if (index < len - 1) {
this.parent.children.splice(index, 1);
this.parent.children.splice(index + 1, 0, this);
this.parent._setChildrenIndices();
return true;
}
return false;
Expand All @@ -1402,7 +1409,6 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
if (index > 0) {
this.parent.children.splice(index, 1);
this.parent.children.splice(index - 1, 0, this);
this.parent._setChildrenIndices();
return true;
}
return false;
Expand All @@ -1422,7 +1428,6 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
if (index > 0) {
this.parent.children.splice(index, 1);
this.parent.children.unshift(this);
this.parent._setChildrenIndices();
return true;
}
return false;
Expand All @@ -1444,7 +1449,6 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
var index = this.index;
this.parent.children.splice(index, 1);
this.parent.children.splice(zIndex, 0, this);
this.parent._setChildrenIndices();
return this;
}
/**
Expand Down
Loading