From 1f9affd979a726b4e7a2dd764a12af161815574d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9?= Date: Wed, 1 Apr 2026 10:56:37 +0100 Subject: [PATCH] perf(collection): avoid duplicated population of new collections (#11473) --- packages/collection/src/collection.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/collection/src/collection.ts b/packages/collection/src/collection.ts index 787e57f71..9e2930ef4 100644 --- a/packages/collection/src/collection.ts +++ b/packages/collection/src/collection.ts @@ -1023,7 +1023,10 @@ export class Collection extends Map { * 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 extends Map { * ``` */ public toSorted(compareFunction: Comparator = Collection.defaultSort): Collection { - 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() {