aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Viney <richard.viney@gmail.com>2024-10-08 00:16:52 +1300
committerLouis Pilfold <louis@lpil.uk>2024-10-07 18:19:43 +0100
commitf63d960bdc8471de501604f782011a25fbac4124 (patch)
tree83ac3f6ae2a276561c2eb54aea1bc92dbec89128
parent3f821a5755a27945c06d7f67fa00888e4a3e80ee (diff)
downloadgleam_stdlib-f63d960bdc8471de501604f782011a25fbac4124.tar.gz
gleam_stdlib-f63d960bdc8471de501604f782011a25fbac4124.zip
Improve JS string performance by caching the Intl Segmenter
-rw-r--r--CHANGELOG.md2
-rw-r--r--src/gleam_stdlib.mjs5
2 files changed, 6 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 84a76b0..03c850f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,8 @@
- The `bit_array` module gains the `compare` function.
- The `float` modeule gains the `to_precision` function.
- The `try_fold` function in the `iterator` module is now tail recursive.
+- The performance of many functions in the `string` module on the JavaScript
+ target has been improved.
## v0.40.0 - 2024-08-19
diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs
index 50ebb46..1c0754f 100644
--- a/src/gleam_stdlib.mjs
+++ b/src/gleam_stdlib.mjs
@@ -156,9 +156,12 @@ export function graphemes(string) {
}
}
+let segmenter = undefined;
+
function graphemes_iterator(string) {
if (globalThis.Intl && Intl.Segmenter) {
- return new Intl.Segmenter().segment(string)[Symbol.iterator]();
+ segmenter ??= new Intl.Segmenter();
+ return segmenter.segment(string)[Symbol.iterator]();
}
}