Callbacks in Javascript - Time & Space Complexity
When using callbacks in JavaScript, it's important to understand how the time to run your code changes as the input grows.
We want to see how callbacks affect the number of steps the program takes.
Analyze the time complexity of the following code snippet.
function processItems(items, callback) {
for (let i = 0; i < items.length; i++) {
callback(items[i]);
}
}
const data = [1, 2, 3, 4, 5];
processItems(data, item => console.log(item));
This code runs a callback function on each item in an array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that goes through each item in the array.
- How many times: Once for each item in the array, so as many times as the array length.
As the number of items grows, the number of times the callback runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 callback calls |
| 100 | 100 callback calls |
| 1000 | 1000 callback calls |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the number of items.
[X] Wrong: "Callbacks make the code slower in a way that changes the time complexity."
[OK] Correct: Callbacks just run code you give them; the main time depends on how many times you call them, not on callbacks themselves.
Understanding how callbacks affect time helps you explain your code clearly and shows you know how your program grows with input size.
"What if the callback itself contains a loop over the entire array? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of callbacks
A callback is a function given to another function to run after some work is done.Step 2: Match the definition
A function passed as an argument to another function to be called later correctly describes this behavior as a function passed as an argument to be called later.Final Answer:
A function passed as an argument to another function to be called later -> Option BQuick Check:
Callback = function passed to run later [OK]
- Thinking callbacks run immediately
- Confusing callbacks with event listeners only
- Believing callbacks cannot be passed as arguments
Solution
Step 1: Check how the callback is called
The callback function must be called with parentheses: callback()Step 2: Identify correct syntax
function greet(callback) { callback(); } correctly defines a function that takes a callback and calls it with callback()Final Answer:
function greet(callback) { callback(); } -> Option DQuick Check:
Call callback with parentheses to execute [OK]
- Forgetting parentheses when calling callback
- Not passing callback as a parameter
- Using callback without calling it
function sayHello(name, callback) {
callback('Hello ' + name);
}
sayHello('Alice', message => console.log(message));Solution
Step 1: Understand the function call
sayHello is called with 'Alice' and a callback that logs the message.Step 2: Trace the callback execution
The callback receives 'Hello Alice' and logs it to the console.Final Answer:
Hello Alice -> Option AQuick Check:
Callback logs 'Hello Alice' [OK]
- Expecting variable names printed instead of values
- Confusing callback parameter with string 'message'
- Thinking callback is not called
function processData(data, callback) {
callback(data);
}
processData('Test');Solution
Step 1: Check function call arguments
processData expects two arguments: data and callback function.Step 2: Identify missing argument
Only one argument ('Test') is passed, so callback is undefined causing error.Final Answer:
Missing callback function when calling processData -> Option CQuick Check:
Callback argument missing causes error [OK]
- Not passing callback argument
- Assuming callback is optional without default
- Ignoring function parameter requirements
setTimeout to print 'Done' after 1 second?Solution
Step 1: Understand setTimeout usage
setTimeout expects a function as first argument and delay in ms as second.Step 2: Check each option
setTimeout(() => console.log('Done'), 1000); passes a function that logs 'Done' after 1000ms delay correctly.Final Answer:
setTimeout(() => console.log('Done'), 1000); -> Option AQuick Check:
Callback function with delay = correct usage [OK]
- Calling function immediately instead of passing it
- Missing delay argument
- Passing wrong argument types
Ready to prove it?
Pass the test and this pattern moves unknown
