Object.create usage in Javascript - Time & Space Complexity
Let's explore how using Object.create affects the time it takes for a program to run.
We want to know how the work grows when we create many objects this way.
Analyze the time complexity of the following code snippet.
const proto = { greet() { return 'Hello'; } };
function createObjects(n) {
const arr = [];
for (let i = 0; i < n; i++) {
arr.push(Object.create(proto));
}
return arr;
}
This code creates an array of n objects, each linked to the same prototype.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
forloop runsntimes, creating one object each time. - How many times: Exactly once per object, so
ntimes total.
As n grows, the number of 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 the number of objects you want to create.
Time Complexity: O(n)
This means the time to create objects grows in a straight line with how many objects you make.
[X] Wrong: "Using Object.create is instant and does not depend on how many objects we create."
[OK] Correct: Each call to Object.create makes a new object, so more objects mean more work and more time.
Understanding how object creation scales helps you write efficient code and explain your choices clearly in interviews.
"What if we replaced the for loop with a recursive function creating objects? How would the time complexity change?"
Practice
Object.create(proto) do in JavaScript?Solution
Step 1: Understand Object.create purpose
Object.create(proto)creates a new object that usesprotoas its prototype, meaning it inherits properties and methods fromproto.Step 2: Differentiate from copying
It does not copy properties but links the new object toprotovia the prototype chain.Final Answer:
Creates a new object withprotoas its prototype. -> Option BQuick Check:
Object.create(proto) sets prototype = proto [OK]
- Thinking it copies properties instead of setting prototype
- Confusing with Object.assign or spread operator
- Assuming it creates arrays or deletes objects
child with prototype parent using Object.create?Solution
Step 1: Recall Object.create syntax
The correct syntax isObject.create(proto)which returns a new object withprotoas prototype.Step 2: Check each option
const child = Object.create(parent); uses correct syntax. const child = Object.create = parent; wrongly assigns. const child = new Object.create(parent); wrongly usesnew. const child = Object.create(parent)(); tries to call the result as a function.Final Answer:
const child = Object.create(parent); -> Option CQuick Check:
Correct syntax = Object.create(proto) [OK]
- Using new with Object.create
- Assigning Object.create to a variable incorrectly
- Calling the result as a function
const parent = { greet() { return 'Hello'; } };
const child = Object.create(parent);
console.log(child.greet());Solution
Step 1: Understand prototype inheritance
childis created withparentas prototype, so it inheritsgreetmethod.Step 2: Calling child.greet()
Sincegreetexists onparent,child.greet()calls that method and returns 'Hello'.Final Answer:
'Hello' -> Option AQuick Check:
Inherited method call returns 'Hello' [OK]
- Expecting undefined because child has no own greet
- Thinking it causes error without own method
- Confusing prototype chain lookup
const proto = { x: 10 };
const obj = Object.create();
console.log(obj.x);Solution
Step 1: Check Object.create usage
Object.create()without arguments is invalid because it requires a prototype object or null.Step 2: Understand consequence
Since no prototype is given, this causes a TypeError or unexpected behavior.Final Answer:
Object.create() requires a prototype argument. -> Option AQuick Check:
Object.create(proto) needs proto argument [OK]
- Calling Object.create without arguments
- Expecting default prototype if none given
- Ignoring TypeError from missing argument
child with prototype parent and add a new property age with value 25 during creation using Object.create?Solution
Step 1: Understand Object.create second argument
The second argument toObject.createis a property descriptor object where each property is defined with descriptors likevalue,writable,enumerable, andconfigurable.Step 2: Check each option
const child = Object.create(parent, { age: { value: 25, writable: true, enumerable: true, configurable: true } }); correctly uses property descriptor forage. const child = Object.create(parent); child.age = 25; adds property after creation, not during. Options A and C use incorrect descriptor syntax.Final Answer:
const child = Object.create(parent, { age: { value: 25, writable: true, enumerable: true, configurable: true } }); -> Option DQuick Check:
Use property descriptors in second argument [OK]
- Adding properties directly in second argument without descriptors
- Adding properties after creation instead of during
- Using wrong syntax for property descriptors
Ready to prove it?
Pass the test and this pattern moves unknown
