diff options
author | inoas <mail@inoas.com> | 2023-06-22 15:52:43 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-22 16:52:43 +0100 |
commit | 6a097950fb48ece2a825aee33d043624967e8b76 (patch) | |
tree | b8bf07bc4e3febf54a7093c7b4db4c4ced3675b5 /src/gleam_stdlib.mjs | |
parent | f4e41c423378175a61dd2c081e7320f41da4a235 (diff) | |
download | gleam_stdlib-6a097950fb48ece2a825aee33d043624967e8b76.tar.gz gleam_stdlib-6a097950fb48ece2a825aee33d043624967e8b76.zip |
improve JS string.join speed (#468)
Diffstat (limited to 'src/gleam_stdlib.mjs')
-rw-r--r-- | src/gleam_stdlib.mjs | 54 |
1 files changed, 36 insertions, 18 deletions
diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs index 4fd06d9..6af9aff 100644 --- a/src/gleam_stdlib.mjs +++ b/src/gleam_stdlib.mjs @@ -196,8 +196,26 @@ export function split(xs, pattern) { return List.fromArray(xs.split(pattern)); } -export function join(xs) { - return xs.toArray().join(""); +export function join(xs, separator) { + let result = ""; + let i = 0; + for (const x of xs) { + if (i == 0) { + result = x + } else { + result = result + separator + x; + } + i++; + } + return result; +} + +export function concat(xs) { + let result = ""; + for (const x of xs) { + result = result + x; + } + return result; } export function length(data) { @@ -497,14 +515,14 @@ function uint6ToB64(nUint6) { return nUint6 < 26 ? nUint6 + 65 : nUint6 < 52 - ? nUint6 + 71 - : nUint6 < 62 - ? nUint6 - 4 - : nUint6 === 62 - ? 43 - : nUint6 === 63 - ? 47 - : 65; + ? nUint6 + 71 + : nUint6 < 62 + ? nUint6 - 4 + : nUint6 === 62 + ? 43 + : nUint6 === 63 + ? 47 + : 65; } // From https://developer.mozilla.org/en-US/docs/Glossary/Base64#Solution_2_%E2%80%93_rewrite_the_DOMs_atob()_and_btoa()_using_JavaScript's_TypedArrays_and_UTF-8 @@ -512,14 +530,14 @@ function b64ToUint6(nChr) { return nChr > 64 && nChr < 91 ? nChr - 65 : nChr > 96 && nChr < 123 - ? nChr - 71 - : nChr > 47 && nChr < 58 - ? nChr + 4 - : nChr === 43 - ? 62 - : nChr === 47 - ? 63 - : 0; + ? nChr - 71 + : nChr > 47 && nChr < 58 + ? nChr + 4 + : nChr === 43 + ? 62 + : nChr === 47 + ? 63 + : 0; } // From https://developer.mozilla.org/en-US/docs/Glossary/Base64#Solution_2_%E2%80%93_rewrite_the_DOMs_atob()_and_btoa()_using_JavaScript's_TypedArrays_and_UTF-8 |