Map method in Javascript - Time & Space Complexity
We want to understand how the time it takes to run the map method changes as the input array gets bigger.
How does the work grow when we apply a function to each item in a list?
Analyze the time complexity of the following code snippet.
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(num => num * 2);
console.log(doubled);
This code creates a new array by doubling each number from the original array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
mapmethod runs a function on each item in the array. - How many times: It runs exactly once for every element in the array.
As the array gets bigger, the number of times the function runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 function calls |
| 100 | 100 function calls |
| 1000 | 1000 function calls |
Pattern observation: The work grows directly with the number of items. Double the items, double the work.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the number of items in the array.
[X] Wrong: "The map method runs faster than a simple loop because it is built-in."
[OK] Correct: Both map and a loop do the same amount of work for each item, so their time grows the same way.
Understanding how map scales helps you explain your code choices clearly and shows you know how to think about performance.
"What if the function inside map called another loop over the array? How would the time complexity change?"
Practice
What does the map method do in JavaScript?
Solution
Step 1: Understand the purpose of
Themapmapmethod applies a function to each element and creates a new array with the results.Step 2: Compare with other array methods
Unlikefilterorsort,mapdoes not remove or reorder elements but transforms them.Final Answer:
Creates a new array by applying a function to each element of the original array. -> Option CQuick Check:
map creates new transformed array = A [OK]
- Thinking map changes the original array
- Confusing map with filter or sort
- Believing map removes elements
Which of the following is the correct syntax to double each number in the array nums using map?
const nums = [1, 2, 3];
Solution
Step 1: Check arrow function syntax inside map
The function must bex => x * 2to return the doubled value directly.Step 2: Identify syntax errors in other options
const doubled = nums.map(x * 2); misses the arrow function syntax. const doubled = nums.map(x => { x * 2 }); uses braces but no return statement, so returns undefined. const doubled = nums.map(x => return x * 2); incorrectly uses return without braces.Final Answer:
const doubled = nums.map(x => x * 2); -> Option AQuick Check:
Correct arrow function syntax = B [OK]
- Missing arrow (=>) in the function
- Using braces without return statement
- Incorrect return syntax inside arrow function
What is the output of the following code?
const arr = [1, 2, 3]; const result = arr.map(x => x + 1); console.log(result);
Solution
Step 1: Understand the map function applied
Each element x in arr is increased by 1, so 1 becomes 2, 2 becomes 3, and 3 becomes 4.Step 2: Check the output array
The new array is [2, 3, 4], which is logged to the console.Final Answer:
[2, 3, 4] -> Option BQuick Check:
Each element +1 = [2, 3, 4] [OK]
- Thinking original array changes
- Confusing output with original array
- Expecting error due to syntax
Find the error in this code snippet:
const numbers = [1, 2, 3];
const newNumbers = numbers.map(num => { num * 3 });
console.log(newNumbers);Solution
Step 1: Analyze arrow function with braces
When using braces in arrow functions, an explicitreturnis needed to return a value.Step 2: Identify missing return
The function body{ num * 3 }does not return anything, sonewNumberswill be an array of undefined.Final Answer:
Missing return statement inside the arrow function block. -> Option DQuick Check:
Arrow function with braces needs return [OK]
- Forgetting return inside arrow function with braces
- Thinking map changes original array
- Assuming map works only with simple functions
You have an array of objects representing people:
const people = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Carol', age: 35 }
];
// Use map to create an array of greetings like "Hello, Alice!"Which code correctly creates this greetings array?
Solution
Step 1: Use map to transform each object to a greeting string
The function should return a string like "Hello, Alice!" using template literals.Step 2: Check arrow function syntax for returning string
const greetings = people.map(person => `Hello, ${person.name}!`); uses implicit return with template literal correctly. const greetings = people.map(person => { `Hello, ${person.name}!` }); misses return inside braces. const greetings = people.map(person => { return `Hello, ${person.age}!` }); incorrectly uses age instead of name. const greetings = people.map(person => person.name + 'Hello!'); concatenates incorrectly.Final Answer:
const greetings = people.map(person => `Hello, ${person.name}!`); -> Option AQuick Check:
Implicit return with template literal = C [OK]
- Missing return inside braces
- Incorrect string concatenation
- Confusing implicit and explicit return
Ready to prove it?
Pass the test and this pattern moves unknown
