Running JavaScript using Node.js - Time & Space Complexity
When running JavaScript with Node.js, it's helpful to understand how the time your code takes grows as you give it more work.
We want to see how the number of operations changes when the input size changes.
Analyze the time complexity of the following code snippet.
const fs = require('fs');
function readFiles(fileNames) {
fileNames.forEach(name => {
const content = fs.readFileSync(name, 'utf8');
console.log(content.length);
});
}
readFiles(['file1.txt', 'file2.txt', 'file3.txt']);
This code reads several files one by one and prints their length.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each file synchronously inside a loop.
- How many times: Once for each file in the input list.
As you add more files, the total time grows roughly in direct proportion to the number of files.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 file reads |
| 100 | 100 file reads |
| 1000 | 1000 file reads |
Pattern observation: Doubling the number of files roughly doubles the work done.
Time Complexity: O(n)
This means the time grows in a straight line with the number of files you read.
[X] Wrong: "Reading multiple files at once will always be faster and have constant time."
[OK] Correct: Even if you read files in parallel, the total work still depends on how many files there are, so time grows with input size.
Understanding how your Node.js code scales with input size shows you can write efficient programs and reason about performance clearly.
"What if we changed from reading files synchronously to reading them asynchronously with promises? How would the time complexity change?"
Practice
node filename.js do?Solution
Step 1: Understand the command
This command tells Node.js to run the JavaScript code inside the file namednode filename.jsfilename.js.Step 2: Identify what Node.js does
Node.js executes JavaScript code outside the browser, so it runs the code in the file.Final Answer:
Runs the JavaScript code insidefilename.jsusing Node.js -> Option AQuick Check:
node filename.js runs code = C [OK]
- Thinking it opens the file in an editor
- Confusing running with compiling
- Assuming it deletes the file
Solution
Step 1: Recall how to start Node.js interactively
Typingnodealone in the terminal starts an interactive JavaScript session.Step 2: Check other options
node filename.jsruns a file, not interactive mode;run nodeandstart node.jsare invalid commands.Final Answer:
node -> Option CQuick Check:
Justnodestarts interactive mode [OK]
- Adding extra words like 'run' or 'start'
- Confusing running a file with interactive mode
- Using file names when none are needed
node test.js?console.log('Hello from Node.js!');Solution
Step 1: Understand the code inside
The code usestest.jsconsole.logto print a string to the console.Step 2: Know what Node.js does when running the file
Node.js executes the code and prints the string to the terminal.Final Answer:
Hello from Node.js! -> Option BQuick Check:
console.log prints text = Hello from Node.js! [OK]
- Expecting no output
- Confusing console.log with return value
- Thinking it causes an error
node app.js but get an error: SyntaxError: Unexpected token. What is the most likely cause?Solution
Step 1: Understand the error message
ASyntaxError: Unexpected tokenmeans the JavaScript code has a syntax mistake.Step 2: Check other options
Typingnodewrong usually gives a different error; missing Node.js causes 'command not found'; unsaved file won't run but usually no syntax error.Final Answer:
Theapp.jsfile has invalid JavaScript syntax -> Option DQuick Check:
SyntaxError means code syntax is wrong [OK]
- Assuming Node.js is missing
- Thinking the command was typed wrong
- Not saving file before running
Solution
Step 1: Identify how to run code without a file
Runningnodealone opens an interactive session where you can type JavaScript directly.Step 2: Compare other options
Creating a file is slower;npm startruns scripts defined in package.json, not quick tests; browser console is outside Node.js environment.Final Answer:
Runnodein terminal to open interactive mode and type the expression -> Option AQuick Check:
Interactive mode = quick testing [OK]
- Thinking npm start runs any code
- Believing browser console is Node.js
- Always creating files for small tests
Ready to prove it?
Pass the test and this pattern moves unknown
