Fresh object literals vs variable assignment behavior in Typescript - Performance Comparison
Let's explore how creating new objects or assigning variables affects the time it takes for a program to run.
We want to know how the number of operations changes when we use fresh object literals versus reusing variables.
Analyze the time complexity of the following code snippet.
function createObjects(n: number) {
const arr = [];
for (let i = 0; i < n; i++) {
const obj = { value: i };
arr.push(obj);
}
return arr;
}
This code creates an array of n fresh objects, each with a property value set to the loop index.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a new object literal inside the loop.
- How many times: Exactly
ntimes, once per loop iteration.
Each time we increase n, the loop runs more times, creating more objects.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 object creations |
| 100 | 100 object creations |
| 1000 | 1000 object creations |
Pattern observation: The number of operations grows directly with n. Double the input, double the work.
Time Complexity: O(n)
This means the time to run grows in a straight line as the input size increases.
[X] Wrong: "Assigning the same object variable multiple times is just as slow as creating new objects each time."
[OK] Correct: Reusing the same object variable does not create new objects, so it does less work. Creating fresh objects inside a loop takes more time because each object is new and separate.
Understanding how object creation inside loops affects performance helps you write clearer and more efficient code, a skill valued in many coding challenges and real projects.
What if we moved the object literal outside the loop and reused it? How would the time complexity change?
Practice
let b = a;?Solution
Step 1: Understand variable assignment with objects
Assigning one object variable to another copies the reference, not the object itself.Step 2: Effect of reference copying
Both variables now point to the same object in memory, so changes via one affect the other.Final Answer:
Both variables point to the same object in memory. -> Option DQuick Check:
Object assignment copies reference = A [OK]
- Thinking a new object is created on assignment
- Assuming variables hold independent copies
- Confusing object assignment with primitive assignment
Solution
Step 1: Identify fresh object literal syntax
Using curly braces{}creates a new, fresh object literal.Step 2: Check other options
let obj = Object.assign({}); is incorrect becauseObject.assign({})needs a source object to copy from; let obj = obj2; copies reference; let obj = new Object(obj2); creates an object but with a reference or unexpected behavior.Final Answer:
let obj = { name: 'Alice' }; -> Option CQuick Check:
Fresh object literal uses {} = A [OK]
- Using assignment to copy objects instead of fresh literals
- Misusing Object.assign without source
- Confusing new Object() with fresh literal
let a = { x: 1 };
let b = a;
b.x = 5;
console.log(a.x);Solution
Step 1: Understand reference assignment
Variablebpoints to the same object asa.Step 2: Modify property via
Changingbb.xto 5 changes the shared object property.Step 3: Log
Sincea.xaandbreference the same object,a.xis now 5.Final Answer:
5 -> Option BQuick Check:
Shared reference means change visible = B [OK]
- Expecting original object to remain unchanged
- Confusing object copying with reference assignment
- Assuming primitive copy behavior for objects
let obj1 = { val: 10 };
let obj2 = obj1;
obj2 = { val: 20 };
console.log(obj1.val);Solution
Step 1: Analyze initial assignment
obj2 initially points to the same object as obj1.Step 2: Reassign obj2 to a new object
obj2 now points to a fresh object; obj1 still points to the original.Step 3: Check console output
obj1.val remains 10 because obj1's reference did not change.Final Answer:
obj2 assignment creates a new object, obj1 remains unchanged. -> Option AQuick Check:
Reassignment changes reference, original unaffected = D [OK]
- Thinking obj1 changes when obj2 is reassigned
- Confusing property change with variable reassignment
- Expecting both variables to point to new object
const obj1 = { a: 1 };
const obj2 = ???
obj2.a = 5;
console.log(obj1.a);Solution
Step 1: Understand shallow copy with spread operator
Using{ ...obj1 }creates a fresh object with copied properties.Step 2: Check other options
obj2 = obj1; copies reference; obj2 = Object.assign(obj1); is incorrect usage of Object.assign; obj2 = obj1 as any; is just a type assertion, no copy.Step 3: Confirm independence
Changingobj2.adoes not affectobj1.a.Final Answer:
obj2 = { ...obj1 }; -> Option AQuick Check:
Spread operator creates fresh object copy = C [OK]
- Assigning reference instead of copying
- Misusing Object.assign without target
- Confusing type assertion with copying
Ready to prove it?
Pass the test and this pattern moves unknown
