perf(collection): avoid duplicated population of new collections (#11473)

This commit is contained in:
René
2026-04-01 10:56:37 +01:00
committed by GitHub
parent 339b8b5655
commit 1f9affd979

View File

@@ -1023,7 +1023,10 @@ export class Collection<Key, Value> extends Map<Key, Value> {
* but returns a Collection instead of an Array.
*/
public toReversed() {
return new this.constructor[Symbol.species](this).reverse();
const entries = [...this.entries()];
entries.reverse();
return new this.constructor[Symbol.species](entries);
}
/**
@@ -1039,7 +1042,10 @@ export class Collection<Key, Value> extends Map<Key, Value> {
* ```
*/
public toSorted(compareFunction: Comparator<Key, Value> = Collection.defaultSort): Collection<Key, Value> {
return new this.constructor[Symbol.species](this).sort(compareFunction);
const entries = [...this.entries()];
entries.sort((a, b): number => compareFunction(a[1], b[1], a[0], b[0]));
return new this.constructor[Symbol.species](entries);
}
public toJSON() {