Default exports vs named exports in Typescript - Performance Comparison
We want to see how the way we export code affects how long it takes to load or use modules.
How does choosing default or named exports change the work done when importing?
Analyze the time complexity of importing modules using default and named exports.
// default export example
import myFunction from './module';
// named export example
import { myFunction } from './module';
// module.ts
export default function myFunction() { /* ... */ }
// export { myFunction }; // This line is incorrect and removed
export function helper() { /* ... */ }
This code shows two ways to import functions from a module: one uses a default export, the other uses named exports.
Look at what happens when the module is imported.
- Primary operation: Module loading and symbol resolution.
- How many times: Once per import statement during program start or dynamic import.
As the number of exports grows, the time to find the right export may increase.
| Number of Exports (n) | Approx. Operations |
|---|---|
| 10 | 10 lookups |
| 100 | 100 lookups |
| 1000 | 1000 lookups |
Pattern observation: Finding a named export takes longer as exports increase; default export is a single direct reference.
Time Complexity: O(n)
This means the time to find a named export grows linearly with the number of exports, while default export access is constant time.
[X] Wrong: "Default and named exports have the same loading speed no matter how many exports exist."
[OK] Correct: Named exports require searching through all exports, so more exports mean more work; default export is a direct reference.
Understanding how module exports affect loading helps you write efficient code and explain trade-offs clearly in interviews.
What if we used dynamic imports instead of static imports? How would the time complexity change?
Practice
default export and named exports in TypeScript?Solution
Step 1: Understand default export purpose
Default export is designed to export a single main value from a file, like a main function or class.Step 2: Understand named exports purpose
Named exports allow exporting multiple values by their names, so you can import only what you need.Final Answer:
Default export allows exporting one main value per file, named exports allow multiple exports by name. -> Option BQuick Check:
Default export = one main, Named exports = multiple [OK]
- Confusing import syntax for default and named exports
- Thinking default export can export multiple items
- Assuming named exports require default keyword
utils.ts?Solution
Step 1: Recall default import syntax
Default exports are imported without curly braces using the syntax:import name from 'module'.Step 2: Check options for correct syntax
import utils from './utils'; matches this syntax exactly, importing the default export asutils.Final Answer:
import utils from './utils'; -> Option AQuick Check:
Default import has no braces [OK]
- Using curly braces for default imports
- Confusing import * as with default import
- Using require syntax in ES modules
mathUtils.ts:
export default function add(a: number, b: number) { return a + b; }
export function multiply(a: number, b: number) { return a * b; }
What will be the output of this code?
import add, { multiply } from './mathUtils';
console.log(add(2, 3));
console.log(multiply(2, 3));Solution
Step 1: Understand import statements
The default export functionaddis imported without braces, and the named exportmultiplyis imported with braces.Step 2: Evaluate function calls
add(2, 3)returns 5, andmultiply(2, 3)returns 6, so console logs 5 and 6.Final Answer:
5 and 6 -> Option DQuick Check:
Default + named import works = 5 and 6 [OK]
- Importing default export with braces
- Calling undefined functions due to wrong import
- Confusing output values
export default function greet() { return 'Hello'; }
export function greet() { return 'Hi'; }
What is the problem here?Solution
Step 1: Identify duplicate function names
Both the default export and named export use the same function namegreet, which causes a conflict.Step 2: Understand export rules
Exports must have unique names; default export can be anonymous or named but cannot clash with named exports.Final Answer:
You cannot have two functions with the same name exported, even if one is default. -> Option CQuick Check:
Duplicate export names cause error [OK]
- Assuming default export must be anonymous
- Thinking named exports can't be functions
- Ignoring duplicate export names
shapes.ts exporting multiple shapes and a default export for the main shape:
export interface Circle { radius: number; }
export interface Square { side: number; }
const defaultShape = { type: 'circle', radius: 5 };
export default defaultShape;
How would you import both the default shape and the Square interface correctly in another file?Solution
Step 1: Import default export without braces
The default exportdefaultShapeis imported without curly braces.Step 2: Import named export with braces
The named exportSquareinterface is imported with curly braces.Final Answer:
import defaultShape, { Square } from './shapes'; -> Option AQuick Check:
Default no braces, named with braces [OK]
- Using braces for default import
- Trying to import default as named
- Confusing import * as with default import
Ready to prove it?
Pass the test and this pattern moves unknown
