FieldedText TypeScript Library
    Preparing search index...

    Count Records Example

    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.

    1. seekEnd(): Fast-forward to the end of the file to get record count
    2. Performance: Much faster than read() loop for large files
    3. Record counting: Get total records without processing data
    4. Meta deserialization: Load metadata from XML text via FtXmlMetaSerialization.deserialize()
    npx tsx examples/count-records/index.ts
    
    Record count: 10
    

    Use seekEnd() when you:

    • Only need to know the total number of records
    • Want to display progress indicators (e.g., "Processing record X of Y")
    • Need to pre-allocate arrays or buffers based on record count
    • Want to validate file size before processing

    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}`);