•
6 min read
IO Lists: Build Strings Without Copying Them
How Elixir Assembles Text With Nested Lists Instead of Concatenation

At the end of Blog #22, the canvas was turning every point on the board into a <rect> string and stitching those strings into one SVG document. That works, but it asks Elixir to do far more copying than the code suggests.

Bruce shows the problem with something much smaller than a game board, an email address in three pieces:

username = "bruce"
connector = "@"
server = "example.com"

combined = username <> connector <> server
IO.puts(combined)

Reading that line, most of us would count one new string: three pieces went in, one address came out. Bruce counts differently, because each <> is a function call that returns a brand new binary:

“And you might say, that’s only one more copy. But that’s not actually what you’re getting… So what you have is actually five distinct strings with two that I’m just going to throw away after I print something.”

— Bruce Tate

The first call joins username and connector into a new string. The second joins that result and server into another one. Elixir copies bytes into each, prints the final value, and leaves the rest as garbage.

Why Joining Means Copying

This is where Blog #21 pays off. A string in Elixir is a binary, bytes packed back to back in memory. There is no pointer between "bruce" and "@" that can be rewired, and immutable data can’t be extended in place. The only way to get one contiguous row of bytes is to allocate a new row and copy every byte into it.

For one email address, nobody will notice. Templates are a different story. The compiler can flatten a single line like the one above, but real text is rarely built in one line. It gets built in loops, in helper functions, and across nested components, and every step that joins a fragment onto a growing string copies the bytes that came before it.

🎯 Join Groxio's Newsletter

Weekly lessons on Elixir, system design, and AI-assisted development -- plus stories from our training and mentoring sessions.

We respect your privacy. No spam, unsubscribe anytime.

Hand IO a List Instead

The IO module was designed with this cost in mind. Instead of joining the pieces, you put them in a list:

IO.puts([username, connector, server])
#=> bruce@example.com

The output is identical, but nothing was concatenated along the way. Bruce describes what Elixir actually does:

“Because all elixir has to do is drop these characters, then this character, and then these characters.”

— Bruce Tate

Rendering text is not “build one big string, then write it.” It’s “write these bytes, then these, then these.” The final string only has to exist at the destination, whether that’s a terminal, a file, or a socket. The saving is in assembly: you skip the concatenation copies, even though the output device may still buffer or copy data while it writes.

A list like this is called an IO list (you’ll also see the broader term iodata in the docs). It can hold binaries, integers between 0 and 255, and other IO lists. That last part matters most, because it means IO lists nest.

Nest the Fragments Where They Belong

Here is a canvas renderer in the style of Blog #22, written with concatenation:

def rect({row, col}) do
  "<rect x=\"" <> to_string(col * 20) <> "\" y=\"" <>
    to_string(row * 20) <> "\" width=\"20\" height=\"20\" />"
end

def svg(points) do
  "<svg width=\"200\" height=\"200\">" <>
    Enum.join(Enum.map(points, &rect/1)) <> "</svg>"
end

And here is the same renderer as an IO list:

def rect({row, col}) do
  [~s(<rect x="), to_string(col * 20), ~s(" y="),
   to_string(row * 20), ~s(" width="20" height="20" />)]
end

def svg(points) do
  [~s(<svg width="200" height="200">), Enum.map(points, &rect/1), "</svg>"]
end

The change is small: <> becomes a comma, Enum.join/1 disappears, and each fragment goes into a list exactly where it belongs. The list of rectangles sits inside the outer list without being flattened or joined. Fixed fragments like ~s(" width="20" height="20" />) are literals that every call shares, so nothing copies them into each new rectangle string.

When an API truly needs one binary, convert once, at the edge:

points |> svg() |> IO.iodata_to_binary()

The rule to remember is simple: build text as a list, and turn it into a string only when something outside your code insists on one.

Why Phoenix Rendering Stays Fast

You already use this every day. A Phoenix template looks like one big HTML string with values interpolated into it, but it doesn’t compile to concatenation. It compiles to an IO list. The static HTML fragments stay as shared literals, the dynamic values sit in the list next to them, and the whole nested structure goes to the socket. Phoenix never assembles the full page as a single string just to send it.

Bruce explains why this matters well beyond printing email addresses:

“Most of what we do is templated technology. And these templates have this concatenation design pattern over and over and over. And if I am allowed to implement those ideas with IO lists instead I can save a tremendous amount in memory.”

— Bruce Tate

Web pages, JSON responses, and SVG frames all have that shape: a lot of fixed text with a few changing values in between. Bruce points to a second benefit too. When a server renders the same page over and over, it writes the same fragments again and again, so some of them may stay hot in the hardware cache.

Keep the Pieces Apart

The next time you write a renderer, ask a different question: which parts can stay separate until output? Once you start asking it, you stop gluing fragments together because <> is easy to type. Instead, you place them in a list and let the IO layer write them in order.

That ends Module 6’s look at how Elixir stores data. Next we turn to functions, and to the idea that a function is a value you can pass around like any other. That’s Blog #24, “Functions as Data.”


📚 Build Elixir Text From Clear Mental Models

This comes from our Learning Elixir course, where Bruce teaches the mental models behind Elixir's data structures through real-world scenarios. Learn how binaries, nested IO lists, and rendering boundaries help you assemble text without repeated concatenation copies. Available via monthly subscription -- try it for one month with no annual commitment.

— Paulo & Bruce

Bruce Tate's avatar
Bruce Tate
System architecture expert and author of 10+ Elixir books.
Paulo Valim's avatar
Paulo Valim
Full-stack Elixir developer and educator teaching modern Elixir and AI-assisted development.