perf: container sorting

This commit is contained in:
cha0s 2022-04-29 04:35:48 -05:00
parent 866407e31d
commit c5dbadb66e

View File

@ -240,9 +240,22 @@ export default class Container extends Renderable {
child.sort();
}
}
this.$$children.sort((l, r) => l.$$zIndex - r.$$zIndex);
// Double insertion sort, because mostly-sorted is the usual case.
for (let i = 1; i < this.$$children.length; i++) {
const current = this.$$children[i];
const currentChild = this.container.isFake ? undefined : this.container.children[i];
let j = i - 1;
while ((j > -1) && (current.$$zIndex < this.$$children[j].$$zIndex)) {
this.$$children[j + 1] = this.$$children[j];
if (!this.container.isFake) {
this.container.children = this.$$children.map((child) => child.internal);
this.container.swapChildren(this.container.children[j], this.container.children[j + 1]);
}
j--;
}
this.$$children[j + 1] = current;
if (!this.container.isFake) {
this.container.swapChildren(currentChild, this.container.children[j + 1]);
}
}
}