From 3a5b6eb76618a8b5bb6bca6bfafb6fe9e06fac2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B0=D0=B2=D1=80=D1=91=D0=BD=D0=BE=D0=B2=20=D0=90?= =?UTF-8?q?=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sun, 2 Mar 2014 08:21:11 +0800 Subject: [PATCH] Faster `removeChildren` and `destroyChildren` methods --- .gitignore | 1 + src/Container.js | 28 +++++++++++++++------- test/sandbox.html | 60 ----------------------------------------------- 3 files changed, 20 insertions(+), 69 deletions(-) delete mode 100644 test/sandbox.html diff --git a/.gitignore b/.gitignore index 8ef7a812..307d1bcf 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ node_modules bower_components phantomjs.exe documentation +test/sandbox.html # Numerous always-ignore extensions *.diff diff --git a/src/Container.js b/src/Container.js index c26c3459..fda07c28 100644 --- a/src/Container.js +++ b/src/Container.js @@ -27,17 +27,20 @@ * @memberof Kinetic.Container.prototype */ removeChildren: function() { - var children = this.children, - child; - - while(children.length > 0) { - child = children[0]; + var children = Kinetic.Collection.toCollection(this.children); + var child; + for (var i = 0; i < children.length; i++) { + child = children[i]; + // reset parent to prevent many _setChildrenIndices calls + delete child.parent; + child.index = 0; if (child.hasChildren()) { child.removeChildren(); } child.remove(); } - + children = null; + this.children = new Kinetic.Collection(); return this; }, /** @@ -46,10 +49,17 @@ * @memberof Kinetic.Container.prototype */ destroyChildren: function() { - var children = this.children; - while(children.length > 0) { - children[0].destroy(); + var children = Kinetic.Collection.toCollection(this.children); + var child; + for (var i = 0; i < children.length; i++) { + child = children[i]; + // reset parent to prevent many _setChildrenIndices calls + delete child.parent; + child.index = 0; + child.destroy(); } + children = null; + this.children = new Kinetic.Collection(); return this; }, /** diff --git a/test/sandbox.html b/test/sandbox.html deleted file mode 100644 index 61433390..00000000 --- a/test/sandbox.html +++ /dev/null @@ -1,60 +0,0 @@ - - - - KineticJS Mocha Tests - - - - -
- - -
- - - - - - -