konvajs / konva

Konva.js is an HTML5 Canvas JavaScript framework that extends the 2d context by enabling canvas interactivity for desktop and mobile applications.
http://konvajs.org/
Other
11.07k stars 896 forks source link

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

Open luowenxing opened 4 weeks ago

luowenxing commented 4 weeks ago

Make index of Node readonly to prevent potential remove child with complexity o(n^2)

Note that Konva.Container#removeChildren function comment with these line

reset parent to prevent many _setChildrenIndices calls

That means without reset the parent, Konva.Node#remove would call _setChildrenIndice which will take into extra o(n^2) complexity. It works in this case but failed in other case such as call remove node in a certain loop, which offen ocurrs in increasing draw visible node and decreasing destory invisible node in current viewport.

Thus replace the index property with readonly index is a better way to prevent the unexpected complextiy by frequent operation of a Konva Node.

lavrton commented 4 weeks ago

Do you have a specific sample/demo where it gives performance boost? Currently, it fails tests.