Companion vs top-level functions decision in Kotlin - Performance Comparison
When deciding between companion and top-level functions in Kotlin, it's important to understand how their time complexity behaves.
We want to know how the choice affects the speed of running the functions as the program grows.
Analyze the time complexity of calling companion object functions versus top-level functions.
class Example {
companion object {
fun companionFunction(n: Int): Int {
var sum = 0
for (i in 1..n) {
sum += i
}
return sum
}
}
}
fun topLevelFunction(n: Int): Int {
var sum = 0
for (i in 1..n) {
sum += i
}
return sum
}
This code shows two functions doing the same work: summing numbers from 1 to n.
One is inside a companion object, the other is a top-level function.
Both functions have the same repeating operation:
- Primary operation: A loop that adds numbers from 1 to n.
- How many times: Exactly n times for each function call.
The difference is only where the function lives, not what it does repeatedly.
As n grows, the number of additions grows the same way for both functions.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with n, no matter where the function is placed.
Time Complexity: O(n)
This means the time to run the function grows in a straight line with the input size, whether it is a companion or top-level function.
[X] Wrong: "Companion functions are slower because they are inside a class."
[OK] Correct: The location of the function does not add extra loops or work; the time depends on what the function does, not where it lives.
Understanding that function placement does not change time complexity helps you focus on what really matters: the work inside the function.
This skill shows you can separate design choices from performance impact clearly.
What if the companion function called another function inside the class that also loops over n? How would the time complexity change?
Practice
companion object function in Kotlin?Solution
Step 1: Understand companion object purpose
Companion object functions belong to a class and are called using the class name without needing an instance.Step 2: Compare with other options
Top-level functions are for general helpers, instance methods require an object, and functions inside functions are local functions.Final Answer:
When the function logically belongs to a class and should be called with the class name. -> Option AQuick Check:
Companion functions = class-related [OK]
- Confusing companion functions with instance methods
- Using companion for unrelated helper functions
- Thinking companion functions need an object instance
create inside a Kotlin class MyClass?Solution
Step 1: Recall companion object syntax
In Kotlin, companion functions are declared inside a companion object block within the class.Step 2: Check each option
class MyClass { companion object { fun create() { } } } correctly declares a companion object with a function. Syntaxes like fun companion create() { } and companion fun create() { } are invalid. companion object MyClass { fun create() { } } incorrectly places companion object outside the class.Final Answer:
class MyClass { companion object { fun create() { } } } -> Option BQuick Check:
Companion object syntax = class MyClass { companion object { fun create() { } } } [OK]
- Writing companion fun instead of inside companion object
- Placing companion object outside the class
- Using fun companion instead of companion object
class Example {
companion object {
fun greet() = "Hello from companion"
}
}
fun greet() = "Hello from top-level"
fun main() {
println(Example.greet())
println(greet())
}Solution
Step 1: Identify function calls
Example.greet() calls the companion object function returning "Hello from companion". The top-level greet() returns "Hello from top-level".Step 2: Understand call order in main()
First println prints Example.greet() output, second println prints top-level greet() output.Final Answer:
"Hello from companion" followed by "Hello from top-level" -> Option CQuick Check:
Companion vs top-level calls = "Hello from companion" followed by "Hello from top-level" [OK]
- Assuming name conflict causes error
- Mixing up which greet() is called
- Expecting runtime ambiguity error
class Data {
companion object {
fun info() = "Data info"
}
}
fun main() {
println(info())
}What is the error and how to fix it?
Solution
Step 1: Identify the call to info()
The main function calls info() directly, but info() is inside Data's companion object, so it must be called with Data.info().Step 2: Fix the call
Change println(info()) to println(Data.info()) to access the companion function correctly.Final Answer:
Error: Unresolved reference 'info'. Fix: Call as Data.info() -> Option AQuick Check:
Companion functions need class name prefix [OK]
- Calling companion function without class name
- Thinking companion functions are top-level
- Trying to move companion function outside class
formatDate() used by multiple unrelated classes in your Kotlin project. Which approach is best and why?Solution
Step 1: Analyze function usage
The function is a utility used by many unrelated classes, so it should be accessible without class dependency.Step 2: Choose best location
Top-level functions are ideal for general helpers to avoid unnecessary class coupling. Companion objects tie functions to one class, which is not suitable here.Final Answer:
Define formatDate() as a top-level function in a utility file for easy reuse. -> Option DQuick Check:
General helpers = top-level functions [OK]
- Placing utility in companion object of unrelated class
- Duplicating instance methods in multiple classes
- Using nested objects unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
