Skip to content

improve performance of string.join #822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

- The `debug` function in the `io` module has been deprecated in favour of
the `echo` keyword.
- The performance of `string.append` and `string.concat` have been improved.
- The performance of `string.append`, `string.join`, and `string.concat` have
been improved.

## v0.58.0 - 2025-03-23

Expand Down
20 changes: 16 additions & 4 deletions src/gleam/string.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,23 @@ fn repeat_loop(string: String, times: Int, acc: String) -> String {
/// // -> "home/evan/Desktop"
/// ```
///
@external(javascript, "../gleam_stdlib.mjs", "join")
pub fn join(strings: List(String), with separator: String) -> String {
strings
|> list.intersperse(with: separator)
|> concat
case strings {
[] -> ""
[first, ..rest] -> join_loop(rest, separator, first)
}
}

fn join_loop(
strings: List(String),
separator: String,
accumulator: String,
) -> String {
case strings {
[] -> accumulator
[string, ..strings] ->
join_loop(strings, separator, accumulator <> separator <> string)
}
}

/// Pads the start of a `String` until it has a given length.
Expand Down
11 changes: 0 additions & 11 deletions src/gleam_stdlib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,6 @@ export function split(xs, pattern) {
return List.fromArray(xs.split(pattern));
}

export function join(xs, separator) {
const iterator = xs[Symbol.iterator]();
let result = iterator.next().value || "";
let current = iterator.next();
while (!current.done) {
result = result + separator + current.value;
current = iterator.next();
}
return result;
}

export function concat(xs) {
let result = "";
for (const x of xs) {
Expand Down
32 changes: 31 additions & 1 deletion test/gleam/string_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,27 @@ pub fn repeat_test() {
|> should.equal("")
}

pub fn join_test() {
pub fn join_0_test() {
[]
|> string.join(with: ", ")
|> should.equal("")

[]
|> string.join(with: "-")
|> should.equal("")
}

pub fn join_1_test() {
["Hello"]
|> string.join(with: ", ")
|> should.equal("Hello")

["Hello"]
|> string.join(with: "-")
|> should.equal("Hello")
}

pub fn join_2_test() {
["Hello", "world!"]
|> string.join(with: ", ")
|> should.equal("Hello, world!")
Expand All @@ -171,6 +191,16 @@ pub fn join_test() {
|> should.equal("Hello-world!")
}

pub fn join_3_test() {
["Hello", "there", "world!"]
|> string.join(with: ", ")
|> should.equal("Hello, there, world!")

["Hello", "there", "world!"]
|> string.join(with: "-")
|> should.equal("Hello-there-world!")
}

pub fn trim_test() {
" hats \n"
|> string.trim
Expand Down