# Which to prefer in simple cases, \`map!\` or \`broadcast!\`

**URL:** https://discourse.julialang.org/t/which-to-prefer-in-simple-cases-map-or-broadcast/11415
**Category:** General Usage
**Created:** [June 4, 2018, 3:13pm UTC](https://discourse.julialang.org/t/which-to-prefer-in-simple-cases-map-or-broadcast/11415 "2018-06-04T15:13:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)
#### Post date: [June 4, 2018, 3:13pm UTC](https://discourse.julialang.org/t/which-to-prefer-in-simple-cases-map-or-broadcast/11415/1 "2018-06-04T15:13:22Z")

</div>

In a simple case, such as

```julia
v = collect(1:10)
sqrs = similar(v)
map!(abs2, sqrs, v)

```

would it be better to use `broadcast!`? I am using the `!` form (instead of a `.=` assignment) because this expression could be part of a larger expression. My guess is that `broadcast!` could be preferred if there was a chance that it could be fused with other operations.

---

<div class="post-metadata">

### Author: ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)
#### Post date: [June 4, 2018, 8:51pm UTC](https://discourse.julialang.org/t/which-to-prefer-in-simple-cases-map-or-broadcast/11415/2 "2018-06-04T20:51:24Z")

</div>

I would agree with your guess.

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [June 4, 2018, 8:53pm UTC](https://discourse.julialang.org/t/which-to-prefer-in-simple-cases-map-or-broadcast/11415/3 "2018-06-04T20:53:16Z")

</div>

My understanding is that `broadcast` is for `AbstractArray`s and that `map` is for general collections. So, roughly speaking, if your collection has a well-defined order, use `broadcast`. In other cases (e.g. `Set` or `Dict`), use `map`.

So, in your case I would agree: `broadcast`.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [June 4, 2018, 9:05pm UTC](https://discourse.julialang.org/t/which-to-prefer-in-simple-cases-map-or-broadcast/11415/4 "2018-06-04T21:05:27Z")

</div>

`map!` and `broadcast!` are indeed basically the same for these one-argument cases. They only way you’ll see a difference is if a type specifically specialized one but not the other — and then it can only ever be a difference in performance. Broadcasting does have the big advantage of fusion.

You’re more likely to see a difference between the non-bang `map` and `broadcast` since they have a very different system of allocating the resulting array: the former uses `similar` for `AbstractArray`s whereas the latter uses broadcast styles.
