Why typed classes matter in Typescript - Performance Analysis
When using typed classes in TypeScript, it is important to understand how the program's work grows as the input changes.
We want to see how the time to create and use typed classes changes when we have more data or objects.
Analyze the time complexity of the following code snippet.
class User {
constructor(public id: number, public name: string) {}
}
function createUsers(n: number): User[] {
const users: User[] = [];
for (let i = 0; i < n; i++) {
users.push(new User(i, `User${i}`));
}
return users;
}
This code creates an array of typed User objects, each with an id and name, for n users.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop creating and adding User objects to the array.
- How many times: Exactly n times, once for each user.
As n grows, the number of User objects created and added grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 User creations and insertions |
| 100 | About 100 User creations and insertions |
| 1000 | About 1000 User creations and insertions |
Pattern observation: The work grows evenly and directly as n increases.
Time Complexity: O(n)
This means the time to create and store users grows in a straight line with the number of users.
[X] Wrong: "Typed classes make the code slower because of extra type checks at runtime."
[OK] Correct: TypeScript types are removed when the code runs, so typed classes do not add runtime cost. The time depends on how many objects you create, not the types.
Understanding how typed classes affect time helps you explain how your code scales and why types are safe without slowing down your program.
"What if we added a nested loop inside the createUsers function to assign friends to each user? How would the time complexity change?"
Practice
Solution
Step 1: Understand typed classes role
Typed classes define what kind of data each property should hold, helping catch mistakes early.Step 2: Identify the benefit
This early error detection happens before running the program, improving code safety.Final Answer:
They help catch errors before running the code. -> Option CQuick Check:
Typed classes catch errors early = B [OK]
- Thinking typed classes speed up code execution
- Believing typed classes remove the need for functions
- Assuming typed classes allow ignoring data rules
Solution
Step 1: Recall TypeScript property syntax
In TypeScript, properties are declared with a colon between name and type, likename: string;.Step 2: Check each option
class User { name: string; } uses correct syntax. Options A, B, and C use invalid syntax.Final Answer:
class User { name: string; } -> Option BQuick Check:
Property declaration uses colon = D [OK]
- Using equals sign instead of colon for type
- Placing type before property name
- Using arrows or other symbols instead of colon
class Product {
name: string;
price: number;
constructor(name: string, price: number) {
this.name = name;
this.price = price;
}
display() {
return `${this.name} costs $${this.price}`;
}
}
const item = new Product('Book', 15);
console.log(item.display());Solution
Step 1: Understand class and constructor
The constructor setsnameto 'Book' andpriceto 15.Step 2: Check display method output
The display method returns a string combining name and price exactly as "Book costs $15".Final Answer:
Book costs $15 -> Option AQuick Check:
Display method returns formatted string = C [OK]
- Expecting price to show decimals automatically
- Confusing undefined property values
- Thinking type mismatch causes runtime error here
class Person {
age: number;
constructor(age: string) {
this.age = age;
}
}Solution
Step 1: Compare property and constructor types
The propertyageis a number, but constructor expects a string parameter.Step 2: Fix type mismatch
Change constructor parameter type to number to match property type.Final Answer:
Change constructor parameter type to number -> Option AQuick Check:
Property and parameter types must match = A [OK]
- Ignoring type mismatch between property and parameter
- Changing property type unnecessarily
- Assuming no error when types differ
Solution
Step 1: Consider type safety and maintenance
Typed properties and interfaces help catch errors and make code clear for teamwork.Step 2: Evaluate options
A class with typed properties and an array of user objects typed with an interface ensures safety and clarity. Other options sacrifice safety or clarity.Final Answer:
Use a class with typed properties and an array of user objects typed with an interface. -> Option DQuick Check:
Typed classes + interfaces = best practice [OK]
- Using any type loses type safety
- Ignoring interfaces reduces code clarity
- Typing all properties as strings limits data accuracy
Ready to prove it?
Pass the test and this pattern moves unknown
