# Type container equivalence

**URL:** <https://discourse.julialang.org/t/type-container-equivalence/97588>\
**Category:** General Usage\
**Created:** [April 17, 2023, 8:53pm UTC](https://discourse.julialang.org/t/type-container-equivalence/97588 "2023-04-17T20:53:00Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [April 18, 2023, 10:03am UTC](https://discourse.julialang.org/t/type-container-equivalence/97588/8 "2023-04-18T10:03:07Z")

</div>

> [@anon56330260](#):
>
> If you worry about type stability, then never use `Union` type!

Maybe this is a bit too extreme, but only a bit.  
Whenever you put these unions into containers, type stability can easily be lost. Here’s a small example using only Base types — floats, missings, tuples, and arrays:

> [@Is there any reason to use NaN instead of missing?](https://discourse.julialang.org/t/is-there-any-reason-to-use-nan-instead-of-missing/84396/4):
>
> Yes, there are lots of performance (type stability) reasons to use NaN! The overhead with missing can be more than an order of magnitude even in this simple example: julia\> x\_nan = [([1., 2., 3.],), ([1., 2., 3., NaN],)] julia\> x\_missing = [([1., 2., 3.],), ([1., 2., 3., missing],)] julia\> @btime map(x -\> sum.(x), $x\_nan) 35.578 ns (1 allocation: 80 bytes) julia\> @btime map(x -\> sum.(x), $x\_missing) 378.745 ns (8 allocations: 240 bytes)

Despite the simplicity of this example, the type-stable (float-only) code is 10x faster than float+missing.

---

_[View the full topic](https://discourse.julialang.org/t/type-container-equivalence/97588)._
