Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf(mobile): memoize Intl polyfills used during thread opens #40602

Openabotmaker(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/40602/headopened View on GitHub

Showing 3 changed files with 143 additions and 0 deletions (loading 2).

x
0 of 3 viewed, 3 left
143 additions and 0 deletionsapps/mobile/src/i18n/intl-polyfill-cache.test.tsAdded
View file
@@ -0,0 +1,143 @@
+import { describe, expect, it } from "vite-plus/test";
+​
+import { installIntlPolyfillCache } from "./intl-polyfill-cache";
+​
+describe("installIntlPolyfillCache", () => {
+ it("memoizes Intl constructors, getters, and lookups while preserving spec semantics", () => {
+ installIntlPolyfillCache();
+ installIntlPolyfillCache();
+​
+ const canonical = Intl.getCanonicalLocales("en-us");
+ expect(canonical).toEqual(["en-US"]);
+ canonical.push("mutated");
+ expect(Intl.getCanonicalLocales("en-us")).toEqual(["en-US"]);
+ expect(() => Intl.getCanonicalLocales("not_a_valid_tag!!")).toThrow(RangeError);
+​
+ expect(Intl.getCanonicalLocales(["en", "fr"])).toEqual(["en", "fr"]);
+ expect(() => Intl.getCanonicalLocales(["en\u0000fr"])).toThrow(RangeError);
+​
+ let localeReads = 0;
+ const getterLocales: string[] = new Array(1);
+ Object.defineProperty(getterLocales, 0, {
+ enumerable: true,
+ get() {
+ localeReads += 1;
+ return localeReads === 1 ? "en" : "fr";
+ },
+ });
+ expect(Intl.getCanonicalLocales(["en"])).toEqual(["en"]);
+ expect(Intl.getCanonicalLocales(getterLocales)).toEqual(["en"]);
+ expect(Intl.getCanonicalLocales(getterLocales)).toEqual(["fr"]);