Filipewelton

Filipewelton

Are there any performance differences between imports with or without 'only' property?

import Project Module
import Project.Module, only: [function: 1]

I’m sorry if the question is foolish, but I wanted to know if there is any difference.

Marked As Solved

sodapopcan

sodapopcan

I was actually just writing about this as I used to be confused about it.

I would also add to the answers above that all import does(*) is allow you to call functions from other modules unqualified. The reason there is no performance hit is because the compiler effectively rewrites the calls to their fully qualified form. It doesn’t “bring in” the functions into the module like it does in other languages. ie, you can’t do this:

defmodule Bar do
  def bar, do: "bar"
end

defmodule Foo do
  import Bar
end

Foo.bar() # <- undefined function error

This can be quite surprising if you come from JS/Python/Ruby/Lua etc.

(*) Just to cover my bases: import also implicitly calls require.

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hi @Filipewelton there are no differences in either compile time or runtime performance.

dimitarvp

dimitarvp

I can swear the word “performance” was not in the title when I posted my answer which now looks awfully out of place, lol.

dimitarvp

dimitarvp

Consider this:

defmodule Project.Module
  def function(arg) do
    IO.inspect(arg, label: "function")
  end

  def another_function(arg) do
    IO.inspect(arg, label: "another_function")
  end
end

So then let’s use it:

defmodule User1 do
  import Project.Module

  def do_stuff() do
    function()
    another_function()
  end
end

Both work because when you do import Project.Module you include all its functions in your current scope so you don’t have to prefix them.

But if you do this:

defmodule User2 do
  import Project.Module, only: [function: 1]

  def do_stuff() do
    function() # works.
    another_function() # compile error, `another_function` not found.
    Project.Module.another_function() # full scoping works.
  end
end

Link to docs.

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement