So that running a cell a second time is super fast, instead of a slow compilation process.

from a super interesting talk with Nathan Daly!

Problem

There is an overhead to going from and Expr to a result, because Julia is JIT compiled:

Some things like list comprehensions take a long time:

julia> module M
       data = [23]
       Main.@btime eval(:(a = [sqrt(i) for i in data]))
       end
  168.003 μs (57 allocations: 3.70 KiB)

const data = [23] has the same result

This is because the compilation happens each time. Normally this is fine, the amount of overhead is small, and it doesn't happen in loops. It's an overhead that you feel, and it feels fast!

You do notice it when using sliders. The chain from the bound variable to the result (eg a plot) can be very long, and all of those have to recompile and render before you see the result update. Once all cells have completed running, JS is allowed to send back a new value, and this overhead adds up to a noticeably lower update rate. In computationally simple notebooks, you want your updates at 60fps.

Solution

Wrap all cell expressions inside a function. When a cell needs to be evaluated a second time (without a change to its code), you just call this function.

So

a = [sqrt(i) for i in data]

becomes