Filter method in Javascript - Time & Space Complexity
We want to understand how the time it takes to run the filter method changes as the list gets bigger.
How does the work grow when we filter more items?
Analyze the time complexity of the following code snippet.
const numbers = [1, 2, 3, 4, 5, 6];
const evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers);
This code creates a new list with only the even numbers from the original list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each number to see if it is even.
- How many times: Once for every item in the list.
As the list gets bigger, the filter method checks more items one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the size of the list.
Time Complexity: O(n)
This means the time to filter grows in a straight line with the number of items.
[X] Wrong: "Filter only looks at some items, so it runs faster than the list size."
[OK] Correct: Filter always checks every item to decide if it should keep it, so it takes time proportional to the whole list.
Understanding how filter works helps you explain how your code handles data efficiently, a skill useful in many coding conversations.
"What if we used filter inside another loop over the same list? How would the time complexity change?"
Practice
filter method do?Solution
Step 1: Understand the purpose of filter
The filter method tests each element with a function and keeps only those that return true.Step 2: Compare options with filter behavior
Only Creates a new array with elements that pass a test describes creating a new array with elements that pass the test, which matches filter's behavior.Final Answer:
Creates a new array with elements that pass a test -> Option BQuick Check:
filter creates new filtered array = C [OK]
- Thinking filter changes original array
- Confusing filter with sort or reduce
- Believing filter combines elements
arr for values greater than 10?Solution
Step 1: Check arrow function syntax inside filter
The filter method requires a function that returns true or false. The arrow function must return the condition directly or with a return statement.Step 2: Validate each option's syntax
arr.filter(x => x > 10); uses correct arrow function syntax returning x > 10. arr.filter(x > 10); misses the function. arr.filter(x => {x > 10}); uses braces but no return, so returns undefined. arr.filter(x => x > 10)(); adds extra parentheses causing error.Final Answer:
arr.filter(x => x > 10); -> Option CQuick Check:
Correct arrow function syntax = A [OK]
- Omitting arrow function syntax
- Using braces without return in arrow function
- Adding extra parentheses after filter call
const nums = [5, 12, 8, 130, 44]; const result = nums.filter(num => num > 10); console.log(result);
Solution
Step 1: Identify the filter condition
The filter keeps numbers greater than 10.Step 2: Check each number in the array
5 (no), 12 (yes), 8 (no), 130 (yes), 44 (yes). So filtered array is [12, 130, 44].Final Answer:
[12, 130, 44] -> Option DQuick Check:
Filter > 10 = [12,130,44] [OK]
- Including numbers not greater than 10
- Returning original array instead of filtered
- Confusing filter with map or reduce output
const numbers = [1, 2, 3, 4, 5];
const evens = numbers.filter(num => { num % 2 === 0 });
console.log(evens);Solution
Step 1: Analyze arrow function with braces
When using braces in arrow functions, you must explicitly return a value.Step 2: Check the function body
The function body has expression but no return, so it returns undefined, causing filter to keep no elements.Final Answer:
Missing return statement inside arrow function -> Option AQuick Check:
Arrow function with braces needs return [OK]
- Forgetting return inside arrow function with braces
- Thinking filter is not an array method
- Misunderstanding modulo operator syntax
const people = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 17 },
{ name: 'Charlie', age: 19 },
{ name: 'David', age: 15 }
];
// Filter adults (age 18 or older)
const adults = people.filter(person => person.age >= 18);
console.log(adults);What will be the output?
Solution
Step 1: Understand filter condition on objects
The filter keeps objects where person.age is 18 or more.Step 2: Check each person's age
Alice (25) and Charlie (19) meet the condition. Bob (17) and David (15) do not.Final Answer:
[{ name: 'Alice', age: 25 }, { name: 'Charlie', age: 19 }] -> Option AQuick Check:
Filter adults age >= 18 = Alice, Charlie [OK]
- Filtering by name instead of age
- Including underage people by mistake
- Expecting filter to modify original array
Ready to prove it?
Pass the test and this pattern moves unknown
