Why fn and Types Speed Up
Connect strictness to performance.
Why fn and Types Speed Up is a free Mojo Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Speed Comes From Knowing
Mojo is fast because it knows your types ahead of time. With fn and annotations, the compiler can generate tight machine code. ⚡
Python Decides at Runtime
Plain Python checks an object's type every time it runs an operation. That constant dispatch work is a big part of why it can feel slow.
Static Types Skip the Checks
When a type is fixed at compile time, Mojo drops the runtime type checks. The operation becomes a direct, fast instruction.
fn add(a: Int, b: Int) -> Int:
return a + bfn Requires Types
An fn forces you to declare types, giving the compiler everything it needs to optimize. That requirement is the source of its speed.
fn scale(x: Float64) -> Float64:
return x * 2.0No Boxing of Numbers
Python wraps every number in an object on the heap. A typed Mojo Int lives directly as raw bytes, with no wrapper to unpack.
var count: Int = 42Values Fit in Registers
Because a typed number is just bytes, the CPU can keep it in a fast register instead of chasing pointers through memory.
Inlining Becomes Possible
Knowing the exact types lets the compiler inline small functions, removing call overhead in hot loops that run millions of times.
Loops Get Specialized
A typed loop compiles to a fixed sequence of CPU instructions. The compiler can even unroll or vectorize it for extra throughput.
fn total(n: Int) -> Int:
var s = 0
for i in range(n):
s += i
return sErrors Caught Early
Types catch mismatches at compile time, so the running program spends zero effort guarding against them. Safety and speed arrive together.
def Stays Flexible
A def can skip types and behaves like Python, which is handy but slower. You choose strictness only where speed truly matters.
def greet(name):
print("Hi", name)Strictness Is a Trade
You trade a little flexibility for big speed. In hot paths the deal is great; fn plus types is how Mojo closes the gap with C.
Quick Check
Connect strictness to performance.
Recap
Typed fn code lets Mojo skip dynamic checks, avoid boxing, and emit specialized machine code. Strictness in hot paths is the speed secret. 🎯
Frequently asked questions
Is the “Why fn and Types Speed Up” lesson free?
Yes — the full text of “Why fn and Types Speed Up” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.
What will I learn in “Why fn and Types Speed Up”?
Connect strictness to performance. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Mojo Academy?
No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Why fn and Types Speed Up” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Mojo Academy lesson?
Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Writing a Fair Benchmark
- Why fn and Types Speed Up
- Reading the Numbers Honestly
- Idiomatic Mojo Habits