Why classes are introduced in Javascript - Performance Analysis
We want to understand how using classes affects the time it takes for JavaScript code to run.
Specifically, we ask: does introducing classes change how the program's work grows as input grows?
Analyze the time complexity of the following code snippet.
class Person {
constructor(name) {
this.name = name;
}
greet() {
return `Hello, ${this.name}!`;
}
}
const people = [];
for (let i = 0; i < n; i++) {
people.push(new Person(`Person ${i}`));
}
This code creates n Person objects and stores them in an array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop creating new Person objects.
- How many times: Exactly n times, once per loop iteration.
As n grows, the number of Person objects created grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 object creations |
| 100 | 100 object creations |
| 1000 | 1000 object creations |
Pattern observation: The work grows directly with n, doubling n doubles the work.
Time Complexity: O(n)
This means the time to create all objects grows in a straight line as the number of objects increases.
[X] Wrong: "Using classes makes the code slower because of extra overhead."
[OK] Correct: Creating objects with classes still takes time proportional to how many objects you make, just like plain objects. The class syntax does not add hidden loops or repeated work.
Understanding how classes affect time helps you explain your code choices clearly and shows you know how code structure relates to performance.
"What if we added a method inside the loop that does another loop over a fixed-size array? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of classes
Classes help organize data and functions that belong together, making code easier to manage.Step 2: Compare options with class purpose
Only grouping data and actions matches the reason classes were introduced.Final Answer:
To group data and related actions together in one place -> Option DQuick Check:
Classes group data and actions = D [OK]
- Thinking classes make code run faster
- Believing classes remove variables
- Confusing classes with replacing all functions
Solution
Step 1: Recall class syntax in JavaScript
The correct syntax uses the keywordclassfollowed by the class name and curly braces.Step 2: Check each option
class Car { constructor() { } } uses correct syntax withclass Car { constructor() { } }. class Car() { } has wrong parentheses after class name.Final Answer:
class Car { constructor() { } } -> Option CQuick Check:
Correct class syntax = A [OK]
- Adding parentheses after class name
- Using function syntax instead of class
- Confusing arrow functions with classes
class Dog {
constructor(name) {
this.name = name;
}
bark() {
return `${this.name} says woof!`;
}
}
const dog1 = new Dog('Buddy');
console.log(dog1.bark());Solution
Step 1: Understand class and method usage
The class Dog sets the name property via constructor and bark() returns a string using that name.Step 2: Trace the code execution
dog1 is created with name 'Buddy'. Calling dog1.bark() returns 'Buddy says woof!'.Final Answer:
"Buddy says woof!" -> Option AQuick Check:
dog1.bark() = "Buddy says woof!" [OK]
- Expecting dog1 as name instead of 'Buddy'
- Thinking bark() is undefined
- Confusing method call syntax
class Person {
constructor(name) {
this.name = name;
}
greet() {
console.log('Hello, ' + name);
}
}
const p = new Person('Alice');
p.greet();Solution
Step 1: Check variable usage inside greet()
The method usesnamewithoutthis., so it looks for a local variable which does not exist.Step 2: Identify correct property access
It should usethis.nameto access the instance property.Final Answer:
Missing 'this' before 'name' inside greet() method -> Option AQuick Check:
Use this.name to access property [OK]
- Forgetting 'this' keyword
- Thinking constructor syntax is wrong
- Believing method must return instead of console.log
Solution
Step 1: Understand the benefit of classes for similar objects
Classes provide a blueprint to create many objects with the same properties and methods easily.Step 2: Compare options with class benefits
Only Classes let you create many objects easily with shared structure and behavior correctly states that classes simplify creating many similar objects with shared structure and behavior.Final Answer:
Classes let you create many objects easily with shared structure and behavior -> Option BQuick Check:
Classes simplify creating many similar objects = A [OK]
- Thinking classes speed up code execution
- Believing classes save data automatically
- Assuming classes make objects immutable
Ready to prove it?
Pass the test and this pattern moves unknown
