klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending.
So i know lists boil down to [1 | [2 | []]]. I also know that i can append to a list using something such as
iex(1)> list = [1, 2, 3]
[1, 2, 3]
iex(2)> list ++ [4]
[1, 2, 3, 4]
and I can prepend to a list such as
iex(3)> [4 | list]
[4, 1, 2, 3]
But to achieve what I did before on the line above, i would have to reverse the list, prepend, then reverse again. What if i tried to append a list of items? why would the fastest way be to use something such as Enum.concat or using the ++ to stitch together the two lists and not doing a recursive call that will do just prepend the bits and flip the entire list?
Trending in Discussions
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
New
Hey there,
It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Quite interesting article Google brought me. Didn’t find any mentions about it here.
What do you think in general? Would you use togethe...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Hi there! :wave:
@frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kip
Enum.concat/2for two lists is implemented as:ityonemo
In 99% of the code I write performance is not important so I just use whichever is more expressive for the data I have. For example, compile-time list concatenation? Hell yeah use ++.
However, I believe many general libraries (and the enum module itself in some list building methods) take a prepend then reverse strategy. You’ll see that if you search for Enum.reverse in the elixir code.
gregvaughn
Down in erlang, reversing a list executes a heavily optimized native BIF (built-in-function) because it’s a frequently used feature, so it’s probably not as expensive as you think. That being said, unless your lists are long or you’re in a performance critical part of code, don’t worry too much. Do what’s most expressive.
But if you do need to optimize, be sure to benchmark. For some length of list and algorithm append may be faster, but if the length of the list changes, then prepend-then-reverse may be faster.
I find many times the ordering does not have bearing on the correctness of the code. In those cases I prepend out of habit.
klo
Thank you all for the comments. I think i was just overthinking it..
dimitarvp
It’s very good to be curious – shows intellect.
IMO make a very small Elixir project where you benchmark all the approaches you can think of – and with differently sized lists.
bencheeis an excellent library for this.Definitely do satisfy your curiosity but also do measure because often you’d end up quite surprised.
And, in real projects, absolutely go for what’s more readable as others said.
sorentwo
Or you can look at the fast-elixir benchmark which breaks down the various techniques by list size.
srowley
That’s a great resource. Based on the results with respect to this question, it makes me wonder why
Enum.concat/1is implemented the way that it is.NobbZ
Because it removes one level of nesting, for any kind of enumerable.
hauleth
In general, you should always append to the front of the list and reverse list only when needed, as often one will need to append to list much more often than reading it in order. This is one of the improvements that I have introduced in Sentry some time ago, as breadcrumbs were constructed much more often than these were used (only in case of error).
dimitarvp
Say what now? You worked at Sentry?