Function execution context in Javascript - Time & Space Complexity
When we run a function in JavaScript, the computer creates a special environment called the execution context. We want to understand how the time it takes to run a function changes as the function does more work.
How does the function's work grow when we give it bigger inputs?
Analyze the time complexity of the following code snippet.
function sumArray(arr) {
let total = 0;
for (let i = 0; i < arr.length; i++) {
total += arr[i];
}
return total;
}
This function adds up all numbers in an array and returns the total.
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 every element in the array.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 additions |
| 100 | About 100 additions |
| 1000 | About 1000 additions |
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 size of the input array.
[X] Wrong: "The function runs in the same time no matter how big the array is."
[OK] Correct: Because the function adds each number one by one, more numbers mean more work and more time.
Understanding how function execution time grows helps you write faster code and explain your thinking clearly in interviews. It shows you know how to handle bigger inputs smartly.
"What if we changed the function to sum only the first half of the array? How would the time complexity change?"
Practice
Solution
Step 1: Understand what execution context means
The function execution context is created when a function runs and it keeps track of what happens inside that function.Step 2: Identify what is tracked inside the function
It specifically keeps track of variables and parameters declared or passed inside the function.Final Answer:
Variables and parameters inside the function -> Option AQuick Check:
Execution context tracks variables and parameters [OK]
- Thinking it tracks global browser info
- Confusing with event listeners
- Assuming it tracks network requests
Solution
Step 1: Identify function declaration syntax
Only the syntax starting with the keywordfunctiondefines a function that creates an execution context when called.Step 2: Check other options
Options B, C, and D do not define functions; they are variable assignment or direct statements.Final Answer:
function greet() { console.log('Hi'); } -> Option BQuick Check:
Function declaration usesfunctionkeyword [OK]
- Confusing variable assignment with function definition
- Thinking console.log alone defines a function
- Using if statement as a function
function test() {
let x = 5;
return x + 3;
}
console.log(test());Solution
Step 1: Analyze the function body
The functiontestdeclares a variablexwith value 5 inside its execution context.Step 2: Calculate the return value
The function returnsx + 3, which is 5 + 3 = 8.Final Answer:
8 -> Option CQuick Check:
Return value is 5 + 3 = 8 [OK]
- Confusing return value with variable value
- Expecting undefined because of missing global variable
- Thinking function does not return anything
function add() {
console.log(sum);
let sum = 10 + 5;
}
add();Solution
Step 1: Understand variable hoisting with let
Variables declared withletare not hoisted likevar. Accessing them before declaration causes ReferenceError.Step 2: Check the console.log usage
The code tries to logsumbefore it is declared, causing ReferenceError.Final Answer:
ReferenceError because 'sum' is used before declaration -> Option DQuick Check:
Using let variable before declaration causes ReferenceError [OK]
- Assuming let variables are hoisted like var
- Expecting output 15 without error
- Confusing ReferenceError with SyntaxError
function outer() {
let a = 10;
function inner() {
let b = 20;
return a + b;
}
return inner();
}
console.log(outer());What does this example demonstrate about function execution contexts?
Solution
Step 1: Understand nested function execution contexts
The inner function creates its own execution context but can access variables from the outer function's context due to closure.Step 2: Analyze the return value
The inner function returnsa + bwhereais from outer context andbis local, showing access to outer variables.Final Answer:
Inner function can access variables of outer function's execution context -> Option AQuick Check:
Closures allow inner functions to access outer variables [OK]
- Thinking inner function variables are global
- Assuming outer context is lost immediately
- Believing inner cannot access outer variables
Ready to prove it?
Pass the test and this pattern moves unknown
