Understanding the difference between Enum and Stream in Elixir

Rodrigo Martins
2 min readOct 2, 2023

--

In Elixir’s functional programming universe, Enum and Stream are vital modules that provide ways of working with collections of data. Understanding the difference between the two is crucial to writing efficient and idiomatic code in Elixir.

Enum

The Enum module is probably the most used module when working with collections in Elixir. It provides a rich set of operations for manipulating enumerations, that is, any data structure that implements the Enumerable protocol, such as lists, maps, and ranges.

Example of using Enum:

enum = [1, 2, 3, 4, 5]
sum = Enum.reduce(enum, 0, &(&1 + &2))
IO.puts sum # -> 15

Stream

The Stream module, on the other hand, also allows you to perform operations on collections, but in a lazy way. This means that operations are not executed immediately, but only evaluated when strictly necessary. Streams are useful when working with large or infinite collections, where immediate evaluation of all elements would be impractical.

Example of using Stream:

stream = Stream.map(1..1_000_000, &(&1 * 2)) |> Stream.filter(&(&1 > 100))
first = Stream.first(stream)
IO.puts first # -> 102

Differences

  • Assessment:
    1. Enum performs eager evaluation, meaning that operations are performed immediately.
    2. Stream performs lazy evaluation, only computing values when strictly necessary.
  • Performance:
    1. Enum may be less efficient when dealing with very large collections due to its eager evaluation.
    2. Stream can provide better performance with large collections as it only calculates the necessary values.
  • Memory Usage:
    1. Since Enum evaluates all elements in the collection, it may consume more memory.
    2. Stream, being lazy, generally consumes less memory, processing elements one at a time.
  • Usage:
    1. Enum is suitable for finite and moderately sized collections.
    2. Stream is most appropriate for very large or infinite collections.

Although `Enum` and `Stream` share many similar operations, the differences in how they evaluate collections make a big difference in terms of performance and memory usage. Use `Enum` when you need immediate results and the collection is of moderate size. Choose `Stream` when you are dealing with large collections or when you need lazy processing to build efficient data pipelines.

--

--

Rodrigo Martins

Inovação vem de pessoas que se divertem com seus trabalhos (Dr. Demin)