This example demonstrates how to efficiently count records in a CSV file without reading the full data.
Counts the number of records in a CSV file using the seekEnd() method, which is much faster than reading all records because it doesn't parse field values.
The example deserializes metadata from an XML string before creating the reader.
read() loop for large filesFtXmlMetaSerialization.deserialize()npx tsx examples/count-records/index.ts
Record count: 10
Use seekEnd() when you:
If you need to process records AND count them, use a regular read() loop and check reader.recordCount after reading all records:
while (reader.read()) {
// Process record
}
console.log(`Total records: ${reader.recordCount}`);
// Count Records Example
// Simple example of counting records in a CSV file efficiently
import { FtReader, FtXmlMetaSerialization } from "@pbkware/fielded-text-web";
// Sample CSV data
const csvData = `Name,Age,City
John Doe,30,New York
Jane Smith,25,Los Angeles
Bob Johnson,45,Chicago
Alice Williams,35,Houston
Charlie Brown,28,Phoenix
Diana Prince,42,Philadelphia
Eve Davis,31,San Antonio
Frank Miller,39,San Diego
Grace Lee,27,Dallas
Henry Wilson,33,San Jose`;
// Load meta from XML string
const metaXml = `<?xml version="1.0" encoding="utf-8"?>
<FieldedText HeadingLineCount="1">
<Field Name="Name" />
<Field DataType="Integer" Name="Age" />
<Field Name="City" />
</FieldedText>`;
const meta = FtXmlMetaSerialization.deserialize(metaXml);
// Create reader
const reader = new FtReader(meta, csvData);
// Use seekEnd() instead of reading all records
// seekEnd() is much faster for large files as it doesn't parse field values
reader.seekEnd();
console.log(`Record count: ${reader.recordCount}`);