The goal of Homework 4 is to analyze passenger data from the Titanic dataset (train.csv) while reading the file using streams instead of loading the entire file into memory.
Specifically, the task requires:
- Calculating total fares paid by all passengers.
- Calculating average fare for each passenger class (1st, 2nd, and 3rd).
- Calculating total number of survived and non-survived passengers.
- Calculating total number of survived men, women, and children (under 18 years old).
⚠ Important requirement:
- The dataset must be read using streaming, not by loading the whole file at once.
- Implementation can use Node.js streams,
readline, or external npm packages.
This project implements a Titanic dataset analysis tool in Node.js that processes passenger data from a CSV file using stream-based file reading.
Instead of loading the entire dataset into memory, the application reads the file line-by-line using Node.js streaming utilities. This approach is more memory-efficient and scalable for large datasets.
The project contains:
- CSV streaming utilities – read and parse CSV files using streams.
- Titanic analysis class – performs statistical analysis on the dataset.
- Main script (
index.js) – loads the data and prints calculated statistics.
Project structure:
.
├── index.js # Application entry point
├── train.csv # Titanic dataset
└── src
├── Titanic.js # Dataset analysis class
└── file_handling.js # CSV streaming and parsing utilities
The homework focuses on practicing:
- Node.js Streams – reading files efficiently using streams.
- Memory-efficient data processing – avoiding loading large files into memory.
- CSV parsing – safely splitting CSV rows including quoted values.
- Object-oriented design – encapsulating analysis logic inside a reusable class.
- Modern JavaScript features – ES modules, async/await, private class fields.
The module file_handling.js uses:
fs.createReadStream();
readline.createInterface();This allows the file to be processed line by line.
Example:
const stream = fs.createReadStream(filename);
const rl = readline.createInterface({
input: stream,
crlfDelay: Infinity,
});Each line is processed asynchronously using:
for await (const line of rl)A regular expression is used to correctly split CSV values while respecting quoted commas:
/,(?=(?:(?:[^"]*"){2})*[^"]*$)/
Each row is converted into an object using the dataset headers.
Example row:
{
PassengerId: "1",
Survived: "0",
Pclass: "3",
Name: "Braund, Mr. Owen Harris",
Sex: "male",
Age: "22",
Fare: "7.25"
}The Titanic class performs statistical calculations on the dataset.
getTotalFares();Returns the sum of all passenger fares.
getAvgFaresByClass();Calculates average ticket price for each passenger class.
Example output:
{
"1": 84.15,
"2": 20.66,
"3": 13.68
}
getTotalSurvived();Counts rows where:
Survived === "1"
getTotalSurvivedByGender();Groups survived passengers by gender.
Example:
{
male: 109,
female: 233
}
getTotalSurvivedChildren((ageLimit = 18));Counts passengers younger than the given age limit who survived.
Run the program:
node index.jsExample output:
=== Titanic Dataset Stats ===
Total Fares: 28693.95
Average Fares by Class: { '1': 84.15, '2': 20.66, '3': 13.68 }
Total Survived: 342
Total Survived by Gender: { male: 109, female: 233 }
Total Survived Children (under 18): 61
Run the script:
node index.jsThe application will:
- Stream the CSV file
- Parse each line into objects
- Perform statistical analysis
- Print results to the console
- Node.js 18+
Built-in modules used:
fsreadline
No external npm packages are required.
Status: ✅ Completed
- CSV streaming implemented using Node.js streams.
- Dataset processed line-by-line for better memory efficiency.
- Titanic analysis class provides multiple statistical methods.
- Asynchronous iteration (
for await...of) used for streaming processing.
MIT License
This project demonstrates how to build a stream-based data processing tool in Node.js.
Key highlights:
- Efficient streaming file processing
- Reliable CSV parsing with quoted values
- Clean object-oriented dataset analysis
- Separation between data ingestion and analytics
Using streams makes the implementation scalable and memory-efficient, which is essential when working with large datasets.
Made with ❤️ and JavaScript by Sam Malikin 🎓