Object creation in Javascript - Time & Space Complexity
Let's explore how the time it takes to create objects changes as we create more of them.
We want to know how the work grows when making many objects.
Analyze the time complexity of the following code snippet.
const createObjects = (n) => {
const result = [];
for (let i = 0; i < n; i++) {
const obj = { id: i, value: i * 2 };
result.push(obj);
}
return result;
};
This code creates an array of n objects, each with two properties.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating one object inside the loop.
- How many times: Exactly n times, once per loop iteration.
Each time we increase n, we create more objects one after another.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 objects created |
| 100 | 100 objects created |
| 1000 | 1000 objects created |
Pattern observation: The work grows directly with n; doubling n doubles the work.
Time Complexity: O(n)
This means the time to create objects grows in a straight line with the number of objects.
[X] Wrong: "Creating many objects is instant and does not depend on how many we make."
[OK] Correct: Each object takes some time to make, so more objects mean more total time.
Understanding how object creation scales helps you write efficient code and explain your reasoning clearly in interviews.
"What if we added a nested loop inside to create objects inside objects? How would the time complexity change?"
Practice
name and value "Alice"?Solution
Step 1: Understand object literal syntax
Objects in JavaScript are created using curly braces with key-value pairs inside, like{ key: value }.Step 2: Identify the correct syntax for key-value pair
The keynameand value"Alice"should be inside curly braces separated by a colon.Final Answer:
const obj = { name: "Alice" }; -> Option AQuick Check:
Object literal = curly braces with key: value [OK]
- Using parentheses instead of curly braces
- Using array brackets for objects
- Confusing arrow functions with object creation
Solution
Step 1: Recall empty object syntax
An empty object is created using empty curly braces{}.Step 2: Check each option
const obj = {}; uses{}, which is the correct syntax. Others use parentheses, array brackets, or invalid syntax.Final Answer:
const obj = {}; -> Option AQuick Check:
Empty object = {} [OK]
- Using parentheses instead of curly braces
- Using array brackets for objects
- Trying to use new Object[] which is invalid
const person = { name: "Bob", age: 30 };
console.log(person.name);Solution
Step 1: Understand object property access
Using dot notationperson.nameaccesses the value of the keynamein the object.Step 2: Identify the value of
The value ofnamenameis the string "Bob".Final Answer:
"Bob" -> Option CQuick Check:
person.name = "Bob" [OK]
- Confusing key name with value
- Using brackets without quotes incorrectly
- Expecting age instead of name
const car = { brand = "Toyota", year: 2020 };Solution
Step 1: Check object key-value syntax
In objects, keys and values are separated by a colon:, not an equals sign=.Step 2: Identify the incorrect part
The code usesbrand = "Toyota"which is invalid syntax for object properties.Final Answer:
Using = instead of : for the key brand -> Option BQuick Check:
Object key-value separator = colon : [OK]
- Using = instead of : in object literals
- Thinking semicolon is mandatory inside object
- Confusing variable declaration keywords
student with keys name, age, and grades (an array). Which code correctly creates this object?Solution
Step 1: Verify object literal syntax with multiple keys
Objects use curly braces with keys and values separated by colons, and multiple pairs separated by commas.Step 2: Check each option for correct syntax and data types
const student = { name: "Eva", age: 22, grades: [90, 85, 88] }; correctly uses colons and commas, andgradesis an array. const student = { "name" = "Eva", "age" = 22, "grades" = [90, 85, 88] }; uses equals signs which is wrong. const student = [ name: "Eva", age: 22, grades: [90, 85, 88] ]; uses array brackets instead of curly braces. const student = { name: "Eva", age: "22", grades: "90, 85, 88" }; uses a string for grades instead of an array.Final Answer:
const student = { name: "Eva", age: 22, grades: [90, 85, 88] }; -> Option DQuick Check:
Object with array value uses colon and brackets [OK]
- Using = instead of : for keys
- Using arrays brackets instead of curly braces for objects
- Putting arrays as strings instead of actual arrays
Ready to prove it?
Pass the test and this pattern moves unknown
