0
0
Typescriptprogramming~5 mins

What survives compilation to JavaScript in Typescript - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: What survives compilation to JavaScript
O(n)
Understanding Time Complexity

When TypeScript code is turned into JavaScript, some parts stay and some parts disappear.

We want to know which parts keep running and affect how long the program takes.

Scenario Under Consideration

Analyze the time complexity of the following TypeScript code after compilation.


interface User {
  id: number;
  name: string;
}

function greet(user: User) {
  console.log(`Hello, ${user.name}`);
}

const user = { id: 1, name: "Alice" };
greet(user);
    

This code defines a type, a function using that type, and calls the function.

Identify Repeating Operations

Look for loops or repeated steps that run many times.

  • Primary operation: The function call and console output.
  • How many times: Called once here, but could be many times if used in a loop.
How Execution Grows With Input

Since the type information disappears, only the function and its calls affect time.

Input Size (n)Approx. Operations
1010 function calls and prints
100100 function calls and prints
10001000 function calls and prints

Pattern observation: The work grows directly with how many times the function runs, not with type checks.

Final Time Complexity

Time Complexity: O(n)

This means the program's running time depends on how many times the function is called, not on the TypeScript types.

Common Mistake

[X] Wrong: "TypeScript types slow down the program because they add extra work at runtime."

[OK] Correct: TypeScript types are removed during compilation and do not exist in the JavaScript that runs, so they do not affect runtime speed.

Interview Connect

Knowing what stays after compilation helps you understand what really affects your program's speed and what is just for helping you write better code.

Self-Check

"What if we added a loop inside the function that runs n times? How would the time complexity change?"