Performance: Why modules are needed
This concept affects the initial load time and runtime efficiency by organizing code into reusable, isolated pieces.
Jump into concepts and practice - no test required
// file readModule.js
export function readFile() { /* code */ }
// file writeModule.js
export function writeFile() { /* code */ }
// main.js
import { readFile } from './readModule.js';
// Only needed modules loadconst fs = require('fs'); const path = require('path'); // All code in one big file without separation function readFile() { /* big code block */ } function writeFile() { /* big code block */ } // Many unrelated functions mixed together
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Monolithic script file | N/A | N/A | Blocks rendering until fully loaded | [X] Bad |
| Modular code with imports | N/A | N/A | Loads smaller chunks, faster initial paint | [OK] Good |
mathUtils in Node.js?require() to import modules in CommonJS style.const mathUtils = require('mathUtils'); is valid Node.js syntax.const greet = require('./greet');
console.log(greet('Anna'));greet.js exports a function that returns `Hello, ${name}!`?greet module exports a function that returns a greeting string.greet('Anna') returns Hello, Anna!, which is logged.const utils = require('./utils');
console.log(utils.add(2, 3));
// utils.js content:
// module.exports = {
// add: (a, b) => a + b
// }add function is correctly exported as an object property.utils.add(2, 3) is valid and returns 5.math.js exports functions add and multiply, and app.js imports them. How does using modules help when your project grows larger?add and multiply across files without rewriting.