mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
Optimize the append method to check if the data is already sorted
This commit is contained in:
@@ -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 = () => {
|
||||
|
||||
Reference in New Issue
Block a user