Skip to content

Commit 39b59a0

Browse files
precizSteffenDE
authored andcommitted
Optimize Enum.map |> Enum.filter |> Enum.map pipeline to a for comprehension (#4174)
This single comprehension avoids allocating three intermediate lists and traversing the entries multiple times, resulting in better performance and memory efficiency.
1 parent 0a62eea commit 39b59a0

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

lib/phoenix_live_view/upload.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,11 @@ defmodule Phoenix.LiveView.Upload do
327327

328328
Enum.map(results, fn {_ref, result} -> result end)
329329
else
330-
entries
331-
|> Enum.map(fn entry -> {entry, UploadConfig.entry_pid(conf, entry)} end)
332-
|> Enum.filter(fn {_entry, pid} -> is_pid(pid) end)
333-
|> Enum.map(fn {entry, pid} -> Phoenix.LiveView.UploadChannel.consume(pid, entry, func) end)
330+
for entry <- entries,
331+
pid = UploadConfig.entry_pid(conf, entry),
332+
is_pid(pid) do
333+
Phoenix.LiveView.UploadChannel.consume(pid, entry, func)
334+
end
334335
end
335336
end
336337

0 commit comments

Comments
 (0)