# Should closures be avoided?

**URL:** https://discourse.julialang.org/t/should-closures-be-avoided/96073
**Category:** General Usage
**Tags:** question, closure
**Created:** [March 14, 2023, 5:51pm UTC](https://discourse.julialang.org/t/should-closures-be-avoided/96073 "2023-03-14T17:51:29Z")
**Posts on this page:** 1
**Showing post:** 7

<div class="post-metadata">

### Author: ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)
#### Post date: [March 14, 2023, 8:48pm UTC](https://discourse.julialang.org/t/should-closures-be-avoided/96073/7 "2023-03-14T20:48:44Z")

</div>

Indeed, closures cause bad performance far more often than I’d like—especially because of how nice the syntax for comprehensions and generators is, and how common the pattern of assigning variables inside conditionals is. IMO the objective should be to [improve the boxing situation](https://discourse.julialang.org/t/rfc-some-ideas-to-tackle-15276-performance-of-captured-variables-in-closures/95260), but I can’t blame anyone for following a heuristic of avoiding closures until that happens.

There was a similar thread a few days ago; do you find this helpful?

> [@Are "Closures should be avoided whenever possible" still valid in Julia v1.9+?](https://discourse.julialang.org/t/are-closures-should-be-avoided-whenever-possible-still-valid-in-julia-v1-9/95893/5):
>
> There are two scenarios where Fix1 and Fix2 (and possibly [generic typed partial applicators](https://github.com/uniment/PartialFuns.jl), if humanity survives long enough to see them) offer benefit: To avoid recompilation. To avoid boxing when variables are captured. To illustrate the first benefit—avoiding recompilation—consider the following example: # After warmup julia\> @time let xs=(1, 2.0, 3+0im) reduce((x,y)-\>x+y, map(x-\>x^2, Iterators.filter(x-\>isodd(x), xs))) end; 0.304171 seconds (124.78 k allocations: 8.…

---

_[View the full topic](https://discourse.julialang.org/t/should-closures-be-avoided/96073)._
