Namespace declaration in Typescript - Time & Space Complexity
When we use namespaces in TypeScript, we group code together. It's important to see how this grouping affects how long the program takes to run.
We want to know: does using namespaces change how the program's speed grows as we add more code inside them?
Analyze the time complexity of the following code snippet.
namespace MathUtils {
export function square(n: number): number {
return n * n;
}
export function sum(arr: number[]): number {
let total = 0;
for (const num of arr) {
total += num;
}
return total;
}
}
This code defines a namespace with two functions: one squares a number, the other sums an array of numbers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
forloop inside thesumfunction that goes through each number in the array. - How many times: It runs once for every item in the input 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 time to sum numbers grows directly with how many numbers there are. More numbers mean more additions.
Time Complexity: O(n)
This means the time to run the sum function grows in a straight line with the size of the input array.
[X] Wrong: "Using a namespace makes the code slower because it adds extra steps."
[OK] Correct: Namespaces only group code logically. They don't add loops or repeated work, so they don't change how long the code takes to run.
Understanding how code grouping like namespaces affects performance helps you write clear and efficient programs. This skill shows you can think about both organization and speed.
"What if the sum function called another function inside the loop? How would that affect the time complexity?"
Practice
namespace in TypeScript?Solution
Step 1: Understand what namespaces do
Namespaces group related code together to keep it organized.Step 2: Recognize the benefit of namespaces
They help avoid name clashes by keeping code under one name.Final Answer:
To group related code under one name and avoid name clashes -> Option DQuick Check:
Namespaces = Group code + avoid clashes [OK]
- Thinking namespaces create new variable types
- Confusing namespaces with functions
- Assuming namespaces import libraries
Utils in TypeScript?Solution
Step 1: Recall the correct keyword for namespaces
TypeScript uses the keywordnamespaceto declare namespaces.Step 2: Check the syntax format
The correct syntax isnamespace Name { }, sonamespace Utils { }is correct.Final Answer:
namespace Utils { } -> Option AQuick Check:
Namespace syntax = namespace Name { } [OK]
- Using 'module' instead of 'namespace'
- Adding '=' sign in declaration
- Using 'package' keyword which is invalid
namespace MathOps {
export function add(a: number, b: number) {
return a + b;
}
}
console.log(MathOps.add(3, 4));Solution
Step 1: Understand the namespace and export usage
The functionaddis inside namespaceMathOpsand is exported, so it can be accessed outside.Step 2: Evaluate the function call
CallingMathOps.add(3, 4)returns 3 + 4 = 7.Final Answer:
7 -> Option BQuick Check:
Exported function call returns sum 7 [OK]
- Forgetting to export the function
- Trying to call function without namespace prefix
- Expecting runtime errors due to namespace
namespace Data {
function getData() {
return "info";
}
}
console.log(Data.getData());Solution
Step 1: Check function accessibility inside namespace
The functiongetDatais declared but not exported, so it's private to the namespace.Step 2: Understand why console.log(Data.getData()) fails
SincegetDatais not exported, it cannot be accessed outside the namespace, causing an error.Final Answer:
Function getData is not exported, so it cannot be accessed outside -> Option AQuick Check:
Export missing = access error [OK]
- Assuming all namespace members are public by default
- Thinking namespace name is invalid
- Ignoring export keyword importance
log function from Logger inside App?namespace Logger {
export function log(message: string) {
console.log("LOG: " + message);
}
}
namespace App {
export function run() {
// Call Logger.log here
}
}
App.run();Solution
Step 1: Understand namespace access rules
Namespaces are accessed by their name followed by dot and member name, soLogger.logis correct.Step 2: Check the options for calling the function
OnlyLogger.log("App started")correctly calls the exported function from the Logger namespace.Final Answer:
Logger.log("App started"); -> Option CQuick Check:
Namespace function call = NamespaceName.member [OK]
- Calling function without namespace prefix
- Using invalid syntax like '::'
- Assuming nested namespaces auto-import members
Ready to prove it?
Pass the test and this pattern moves unknown
