Array creation in Javascript - Time & Space Complexity
When we create an array in JavaScript, it's important to know how the time it takes grows as the array gets bigger.
We want to answer: How does the time to make an array change when we add more items?
Analyze the time complexity of the following code snippet.
const size = 1000;
const arr = new Array(size);
for (let i = 0; i < size; i++) {
arr[i] = i * 2;
}
This code creates an array of a given size and fills it with numbers doubled from their index.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that assigns values to each array element.
- How many times: It runs exactly once for each item, so
sizetimes.
As the array size grows, the number of assignments grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 assignments |
| 100 | 100 assignments |
| 1000 | 1000 assignments |
Pattern observation: The work grows directly with the number of items; double the size, double the work.
Time Complexity: O(n)
This means the time to create and fill the array grows in a straight line with the number of elements.
[X] Wrong: "Creating an array of size n is instant and does not depend on n."
[OK] Correct: Even if the array is created with a fixed size, filling each element takes time that grows with n.
Understanding how array creation time grows helps you explain performance when working with lists and data structures in real projects.
"What if we used Array.from() with a mapping function instead of a for-loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand array syntax
In JavaScript, arrays are created using square brackets [].Step 2: Identify correct empty array creation
Only[]creates an empty array; parentheses, braces, or quotes create other types.Final Answer:
const arr = []; -> Option AQuick Check:
Empty array = [] [OK]
- Using parentheses () instead of brackets
- Using curly braces {} which create objects
- Using quotes '' which create strings
Solution
Step 1: Recognize array literal syntax
Arrays use square brackets [] with comma-separated values inside.Step 2: Check each option
Only const numbers = [1, 2, 3]; uses square brackets with numbers separated by commas.Final Answer:
const numbers = [1, 2, 3]; -> Option BQuick Check:
Array with 3 numbers = [1, 2, 3] [OK]
- Using parentheses which create expressions, not arrays
- Using curly braces which create objects
- Using quotes which create strings
const arr = [10, 20, 30]; console.log(arr.length);
Solution
Step 1: Understand array length property
Thelengthproperty returns the number of elements in the array.Step 2: Count elements in the array
The array has 3 elements: 10, 20, and 30, soarr.lengthis 3.Final Answer:
3 -> Option DQuick Check:
Array length = 3 [OK]
- Confusing length with last element value
- Expecting length to be undefined
- Thinking length causes an error
const fruits = ["apple", "banana", "orange";
Solution
Step 1: Check array syntax
Arrays must start with [ and end with ]. The code misses the closing bracket.Step 2: Verify other parts
Double quotes are allowed for strings, commas are present between elements.Final Answer:
Missing closing square bracket ] -> Option CQuick Check:
Array syntax requires matching brackets [OK]
- Forgetting to close the array with ]
- Thinking quotes style causes error
- Assuming arrays only hold numbers
Solution
Step 1: Understand array creation with loop
We want numbers 1 to 5, so loop must run 5 times and add values 1 through 5.Step 2: Analyze each option
const arr = []; for(let i = 1; i <= 5; i++) { arr[i] = i; } assigns values starting at index 1, leaving index 0 empty. const arr = new Array(5); for(let i = 1; i <= 5; i++) { arr[i] = i; } creates array of length 5 but assigns starting at index 1, leaving index 0 empty. const arr = []; for(let i = 0; i < 5; i++) { arr[i] = i + 1; } assigns values 1 to 5 at indexes 0 to 4 correctly. const arr = []; for(let i = 1; i < 5; i++) { arr.push(i); } loops only to 4, missing 5.Step 3: Choose best correct code
const arr = []; for(let i = 0; i < 5; i++) { arr[i] = i + 1; } correctly fills array indexes 0 to 4 with values 1 to 5.Final Answer:
const arr = []; for(let i = 0; i < 5; i++) { arr[i] = i + 1; } -> Option AQuick Check:
Loop 0-4, assign i+1 = 1-5 [OK]
- Starting loop at 1 but assigning to arr[i] leaves arr[0] empty
- Loop ending before 5
- Using new Array(5) without filling all indexes
Ready to prove it?
Pass the test and this pattern moves unknown
