`nil` or empty list?

I’m managing my Postgres database with Ecto and I have come across a situation: In some of the fields for some of my records, I save empty values: For this case, it would be an empty list [].

Empty list or nil, which is better or more efficient?

1 Like

empty list.
See this PR that was an optimization to MapSet.

1 Like

Tangential fun fact: In Erlang, the empty list [] is internally called nil, and it has it’s own position in the erlang term ordering :slight_smile:
https://www.erlang.org/doc/reference_manual/expressions.html#term-comparisons

6 Likes

I would first and foremost prioritize clarity, then performance here, since the difference is negligible in most cases. If you have a column that can either have a value, or no value, then use nil to mean “no value”. If you have a collection of things and that collection can be empty, then use an [].

4 Likes

If you’ve got a JSONB column or a nullable array column, using nil is going to cause the usual SQL NULL behaviors while using [] will not.

4 Likes