Optimize the append method to check if the data is already sorted

This commit is contained in:
2025-07-02 18:43:41 -04:00
parent db506e8b1f
commit 220b402b3b
2 changed files with 33 additions and 3 deletions

View File

@@ -38,6 +38,9 @@ export class CircularQueue<T> {
return;
}
const lastItem = this.get(this._size - 1);
const isAlreadyInOrder = lastItem && sortingCallback(lastItem, data) <= 0;
if (this._size < this._capacity) {
this.endIndex = (this.endIndex + 1) % this._capacity;
this._data[this.endIndex] = data;
@@ -48,7 +51,9 @@ export class CircularQueue<T> {
this._data[this.endIndex] = data;
}
this.sortData(sortingCallback);
if (!isAlreadyInOrder) {
this.sortData(sortingCallback);
}
}
popFront = () => {