So inspect
is going to check a data structure and represent as accurately as possible using Elixir syntax. This means that, if you have a binary, it will check if it is valid utf-8 codepoints, and then format it, looking for escape characters, quotes, etc. There is a chance inspect could be made faster for binaries, I don’t remember anyone particularly optimizing it, but in your case, no work will always be faster than doing any work, and if you already have a binary, you can just log the binary as is with a list.
Actually, I would say inspecting a json blob will actually make things worse, because instead of seeing {"foo": "bar"}
, you will now see "{\"foo\": \"bar\"}"
.
Others have touched important points that arise when measuring code. For example, if you are measuring Elixir scripts, there is no protocol consolidation and that takes the hit. And I would like to add one other tip. If your log call needs to do a good amount of work, pass it an anonymous function:
Logger.info(fn -> “Payload received: #{inspect pl}” end)
The anonymous function will only be invoked if you are interested in the log level. This is specially important for info and debug messages.