diff --git a/packages/core/src/Entity.ts b/packages/core/src/Entity.ts index 47c0e6fa10..dc6034cb01 100644 --- a/packages/core/src/Entity.ts +++ b/packages/core/src/Entity.ts @@ -516,11 +516,13 @@ export class Entity extends EngineObject { const oldParent = this._parent; if (oldParent != null) { const oldSibling = oldParent._children; - let index = this._siblingIndex; - oldSibling.splice(index, 1); - for (let n = oldSibling.length; index < n; index++) { - oldSibling[index]._siblingIndex--; + const count = oldSibling.length - 1; + for (let i = this._siblingIndex; i < count; i++) { + const child = oldSibling[i + 1]; + oldSibling[i] = child; + child._siblingIndex = i; } + oldSibling.length = count; this._parent = null; this._siblingIndex = -1; } @@ -554,17 +556,21 @@ export class Entity extends EngineObject { const children = this._children; const childCount = children.length; if (index === undefined) { + children.length = childCount + 1; child._siblingIndex = childCount; - children.push(child); + children[childCount] = child; } else { if (index < 0 || index > childCount) { throw `The index ${index} is out of child list bounds ${childCount}`; } - child._siblingIndex = index; - children.splice(index, 0, child); - for (let i = index + 1, n = childCount + 1; i < n; i++) { - children[i]._siblingIndex++; + children.length = childCount + 1; + for (let i = childCount; i > index; i--) { + const swapChild = children[i - 1]; + swapChild._siblingIndex = i; + children[i] = swapChild; } + child._siblingIndex = index; + children[index] = child; } } diff --git a/packages/core/src/Scene.ts b/packages/core/src/Scene.ts index 1a4644c1d3..12050faa86 100644 --- a/packages/core/src/Scene.ts +++ b/packages/core/src/Scene.ts @@ -455,11 +455,13 @@ export class Scene extends EngineObject { */ _removeFromEntityList(entity: Entity): void { const rootEntities = this._rootEntities; - let index = entity._siblingIndex; - rootEntities.splice(index, 1); - for (let n = rootEntities.length; index < n; index++) { - rootEntities[index]._siblingIndex--; + const count = rootEntities.length - 1; + for (let i = entity._siblingIndex; i < count; i++) { + const child = rootEntities[i + 1]; + rootEntities[i] = child; + child._siblingIndex = i; } + rootEntities.length = count; entity._siblingIndex = -1; } @@ -489,17 +491,21 @@ export class Scene extends EngineObject { const rootEntities = this._rootEntities; const rootEntityCount = rootEntities.length; if (index === undefined) { + rootEntities.length = rootEntityCount + 1; rootEntity._siblingIndex = rootEntityCount; - rootEntities.push(rootEntity); + rootEntities[rootEntityCount] = rootEntity; } else { if (index < 0 || index > rootEntityCount) { throw `The index ${index} is out of child list bounds ${rootEntityCount}`; } - rootEntity._siblingIndex = index; - rootEntities.splice(index, 0, rootEntity); - for (let i = index + 1, n = rootEntityCount + 1; i < n; i++) { - rootEntities[i]._siblingIndex++; + rootEntities.length = rootEntityCount + 1; + for (let i = rootEntityCount; i > index; i--) { + const swapRoot = rootEntities[i - 1]; + swapRoot._siblingIndex = i; + rootEntities[i] = swapRoot; } + rootEntity._siblingIndex = index; + rootEntities[index] = rootEntity; } }