diff --git a/src/core/sort.js b/src/core/sort.js index 627891ff00e..062667ced3a 100644 --- a/src/core/sort.js +++ b/src/core/sort.js @@ -6,15 +6,6 @@ */ const cmpPriority = (a, b) => a.priority - b.priority; - -/** - * @param {{order: number}} a - First object with `order` property. - * @param {{order: number}} b - Second object with `order` property. - * @returns {number} A number indicating the relative position. - * @ignore - */ -const cmpStaticOrder = (a, b) => a.constructor.order - b.constructor.order; - /** * @param {Array<{priority: number}>} arr - Array to be sorted in place where each element contains * an object with at least a priority property. @@ -22,11 +13,3 @@ const cmpStaticOrder = (a, b) => a.constructor.order - b.constructor.order; * @ignore */ export const sortPriority = arr => arr.sort(cmpPriority); - -/** - * @param {Array<{order: number}>} arr - Array to be sorted in place where each element contains - * an object with a static `order` property. - * @returns {Array<{order: number}>} In place sorted array. - * @ignore - */ -export const sortStaticOrder = arr => arr.sort(cmpStaticOrder); diff --git a/src/framework/entity.js b/src/framework/entity.js index 16a1ad7f47b..9307f27c557 100644 --- a/src/framework/entity.js +++ b/src/framework/entity.js @@ -1,11 +1,30 @@ import { Debug } from '../core/debug.js'; import { guid } from '../core/guid.js'; -import { sortStaticOrder } from '../core/sort.js'; import { GraphNode } from '../scene/graph-node.js'; import { getApplication } from './globals.js'; +/** + * @typedef {import('./components/component.js').Component} Component + */ + +/** + * @param {Component} a - First object with `order` property. + * @param {Component} b - Second object with `order` property. + * @returns {number} A number indicating the relative position. + * @ignore + */ +const cmpStaticOrder = (a, b) => a.constructor.order - b.constructor.order; + +/** + * @param {Array} arr - Array to be sorted in place where each element contains + * an object with a static `order` property. + * @returns {Array} In place sorted array. + * @ignore + */ +const sortStaticOrder = arr => arr.sort(cmpStaticOrder); + /** * @type {GraphNode[]} * @ignore @@ -13,10 +32,23 @@ import { getApplication } from './globals.js'; const _enableList = []; /** - * @type {Array} + * @type {Array>} + * @ignore + */ +const tmpPool = []; + +const getTempArray = () => { + return tmpPool.pop() ?? []; +}; + +/** + * @param {Array} a - Array to return back to pool. * @ignore */ -const _sortedArray = []; +const releaseTempArray = (a) => { + a.length = 0; + tmpPool.push(a); +}; /** * The Entity is the core primitive of a PlayCanvas game. Generally speaking an object in your game @@ -522,7 +554,7 @@ class Entity extends GraphNode { } } - components.length = 0; + releaseTempArray(components); } /** @private */ @@ -533,7 +565,7 @@ class Entity extends GraphNode { components[i].onPostStateChange(); } - components.length = 0; + releaseTempArray(components); } /** @@ -609,23 +641,22 @@ class Entity extends GraphNode { } _getSortedComponents() { - Debug.assert(_sortedArray.length === 0); - const components = this.c; + const sortedArray = getTempArray(); let needSort = 0; for (const type in components) { if (components.hasOwnProperty(type)) { const component = components[type]; needSort |= component.constructor.order !== 0; - _sortedArray.push(component); + sortedArray.push(component); } } - if (needSort && _sortedArray.length > 1) { - sortStaticOrder(_sortedArray); + if (needSort && sortedArray.length > 1) { + sortStaticOrder(sortedArray); } - return _sortedArray; + return sortedArray; } /**