From 588d161a337c232f22aa121f7f9be7268a766299 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 26 Nov 2026 19:05:14 -0500 Subject: [PATCH] perf: Sort titles with Intl.Collator instead of natural-sort (#14766) natural-sort runs a date regex with nested quantifiers on every compare, which made sorting long titles that contain numbers very slow. --- package.json | 2 -- shared/utils/naturalSort.test.ts | 26 ++++++++++++++++++++++--- shared/utils/naturalSort.ts | 33 +++++++++++++++++++++----------- yarn.lock | 16 ---------------- 4 files changed, 45 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index 84b30801ca..3992496303 100644 --- a/package.json +++ b/package.json @@ -179,7 +179,6 @@ "mobx": "^6.16.1", "mobx-react": "^9.2.2", "mobx-utils": "^6.1.1", - "natural-sort": "^1.0.0", "node-fetch": "2.7.0", "nodemailer": "^10.6.0", "octokit": "^5.0.5", @@ -292,7 +291,6 @@ "@types/markdown-it-container": "^2.0.11", "@types/markdown-it-emoji": "^3.0.1", "@types/mime-types": "^3.0.1", - "@types/natural-sort": "^0.0.24", "@types/node": "22.20.5", "@types/node-fetch": "^2.6.13", "@types/passport-oauth2": "^1.8.0", diff --git a/shared/utils/naturalSort.test.ts b/shared/utils/naturalSort.test.ts index 6e51d270e4..51a60a90dc 100644 --- a/shared/utils/naturalSort.test.ts +++ b/shared/utils/naturalSort.test.ts @@ -72,9 +72,6 @@ describe("#naturalSort", () => { caseSensitive: true, }) ).toEqual([ - { - name: "joan", - }, { name: "Pedro", }, @@ -84,6 +81,9 @@ describe("#naturalSort", () => { { name: "Joan", }, + { + name: "joan", + }, ]); }); @@ -137,3 +137,23 @@ describe("#naturalSort", () => { ]); }); }); + +describe("#naturalSort performance", () => { + it("should quickly sort long titles that contain years", () => { + const items = Array.from({ length: 400 }, (_, i) => ({ + title: `Engineering planning notes and retrospective ${2000 + (i % 25)} part ${i}`, + })); + + const start = performance.now(); + const sorted = naturalSort(items, "title"); + const duration = performance.now() - start; + + expect(sorted[0].title).toEqual( + "Engineering planning notes and retrospective 2000 part 0" + ); + expect(sorted[1].title).toEqual( + "Engineering planning notes and retrospective 2000 part 25" + ); + expect(duration).toBeLessThan(500); + }); +}); diff --git a/shared/utils/naturalSort.ts b/shared/utils/naturalSort.ts index 844fc3ac1b..719f5f7a1a 100644 --- a/shared/utils/naturalSort.ts +++ b/shared/utils/naturalSort.ts @@ -1,13 +1,19 @@ import emojiRegex from "emoji-regex"; import { deburr } from "es-toolkit/compat"; -import naturalSort from "natural-sort"; type NaturalSortOptions = { caseSensitive?: boolean; direction?: "asc" | "desc"; }; -const sorter = naturalSort(); +const collator = new Intl.Collator(undefined, { + numeric: true, + sensitivity: "base", +}); +const caseSensitiveCollator = new Intl.Collator(undefined, { + numeric: true, + sensitivity: "case", +}); const regex = emojiRegex(); const stripEmojis = (value: string) => value.replace(regex, ""); @@ -33,17 +39,22 @@ function naturalSortBy( if (!items) { return []; } - const sort = sortOptions - ? naturalSort({ - caseSensitive: sortOptions.caseSensitive, - direction: sortOptions.direction === "desc" ? "desc" : undefined, - }) - : sorter; + const compare = sortOptions?.caseSensitive + ? caseSensitiveCollator.compare + : collator.compare; + const multiplier = sortOptions?.direction === "desc" ? -1 : 1; // Sort a copy, `items` may be an observable array which cannot be sorted in place. - return items - .slice() - .sort((a: T, b: T) => sort(getSortByField(a, key), getSortByField(b, key))); + return items.slice().sort((a: T, b: T) => { + const x = getSortByField(a, key); + const y = getSortByField(b, key); + + // Empty values sort last. + if (!x || !y) { + return (Number(!x) - Number(!y)) * multiplier; + } + return compare(x, y) * multiplier; + }); } export default naturalSortBy; diff --git a/yarn.lock b/yarn.lock index 3350d991d7..66dd3853f9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6592,13 +6592,6 @@ __metadata: languageName: node linkType: hard -"@types/natural-sort@npm:^0.0.24": - version: 0.0.24 - resolution: "@types/natural-sort@npm:0.0.24" - checksum: 10c0/b3ccb4b55819ff265edf311fcd22f49d0e6c84394172fb44eed7b7afdc0c4756f9116ec874daf19c9e7282c9984477e833b4f6f0c3c9741a1e4b88441cf1737b - languageName: node - linkType: hard - "@types/node-fetch@npm:^2.5.10, @types/node-fetch@npm:^2.6.13": version: 2.6.13 resolution: "@types/node-fetch@npm:2.6.13" @@ -13609,13 +13602,6 @@ __metadata: languageName: node linkType: hard -"natural-sort@npm:^1.0.0": - version: 1.0.0 - resolution: "natural-sort@npm:1.0.0" - checksum: 10c0/b1495961f9871aabf526e90fb8e7e4192f7a64a436f1e5b793b1c6b166bc3dcac1081e997e3b9170a71edaa3be6fad6d9a25c261cb0df9cbcdf8c037e3a2e8a4 - languageName: node - linkType: hard - "negotiator@npm:0.6.3": version: 0.6.3 resolution: "negotiator@npm:0.6.3" @@ -14102,7 +14088,6 @@ __metadata: "@types/markdown-it-container": "npm:^2.0.11" "@types/markdown-it-emoji": "npm:^3.0.1" "@types/mime-types": "npm:^3.0.1" - "@types/natural-sort": "npm:^0.0.24" "@types/node": "npm:22.20.5" "@types/node-fetch": "npm:^2.6.13" "@types/passport-oauth2": "npm:^1.8.0" @@ -14195,7 +14180,6 @@ __metadata: mobx-react: "npm:^9.2.2" mobx-utils: "npm:^6.1.1" msw: "npm:^2.15.0" - natural-sort: "npm:^1.0.0" node-fetch: "npm:2.7.0" nodemailer: "npm:^10.6.0" octokit: "npm:^5.0.5"