0
0
Typescriptprogramming~5 mins

Super keyword behavior in Typescript - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Super keyword behavior
O(n)
Understanding Time Complexity

Let's explore how using the super keyword affects the time it takes for a program to run.

We want to know how calling methods from a parent class scales as our program grows.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


class Animal {
  speak() {
    console.log('Animal speaks');
  }
}

class Dog extends Animal {
  speak() {
    super.speak();
    console.log('Dog barks');
  }
}

const dog = new Dog();
dog.speak();
    

This code shows a class Dog that calls its parent Animal method using super.speak().

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Calling super.speak() inside the Dog class method.
  • How many times: Exactly once per dog.speak() call.
How Execution Grows With Input

Since super.speak() calls a single parent method, the work done does not increase with input size.

Input Size (n)Approx. Operations
1010 calls, each calls super.speak() once
100100 calls, each calls super.speak() once
10001000 calls, each calls super.speak() once

Pattern observation: The number of operations grows directly with how many times the method is called, but each call does a fixed amount of work.

Final Time Complexity

Time Complexity: O(n)

This means the total time grows linearly with how many times you call the method, but each call itself is simple and constant time.

Common Mistake

[X] Wrong: "Using super makes the method slower by a lot because it calls another method."

[OK] Correct: Calling super just runs one extra method, which is a fixed cost and does not multiply with input size inside that call.

Interview Connect

Understanding how super works helps you explain class inheritance clearly and shows you know how method calls affect performance in real code.

Self-Check

"What if the parent method called by super itself called another method multiple times? How would that affect the time complexity?"