fast-csv

repository·main·Indexed 23 days ago

https://github.com/c2fo/fast-csv

A high-performance Node.js library for parsing and formatting CSV and other delimited value files. Built with a stream-first architecture to handle large datasets with minimal memory usage, it includes the unified fast-csv package as well as specialized @fast-csv/format and @fast-csv/parse packages. The library is written in TypeScript and provides flexible configuration for delimiters, quote characters, escape characters, and header management.

Tokens
19.7K
Snippets
54
Records
123
Agent score
82%

What's inside fast-csv

  1. Overview of fast-csv features

    main

    Fast-csv is a Node.js library designed for parsing and formatting CSVs or other delimited value files. Key features include:

    • CSV Formatting & Parsing: Comprehensive support for both operations.
    • Stream-First Architecture: Built using Node.js streams to maintain a low memory footprint, making it suitable for processing very large files.
    • Flexible Configuration: Provides extensive formatting and parsing options to handle diverse file scenarios.
    • TypeScript Support: The library is built using TypeScript.
  2. Choose the right fast-csv package

    main

    Depending on your requirements, you can use the monolithic package or specific sub-packages to reduce your dependency footprint:

    • fast-csv: The complete package containing all methods and options from both @fast-csv/format and @fast-csv/parse.
    • @fast-csv/parse: Use this if your application only requires CSV parsing capabilities.
    • @fast-csv/format: Use this if your application only requires CSV formatting capabilities.
  3. Supported row formats for CSV creation

    main

    When using fast-csv formatters to create a CSV, you can provide data in three distinct row formats depending on your requirements for headers and column names:

    1. Object format ({[string]: any}): Pass an array of objects. The keys of the first object are automatically used as the CSV header names.
    2. Array format (string[]): Pass an array of arrays. The first inner array is treated as the header row.
    3. Key-Value Pair format ([string, any][]): Pass an array of arrays where each inner array contains [headerName, value] pairs. This is useful for generating CSVs with duplicate header names.
    // Object format
    [
      { a: "a1", b: "b1", c: "c1" }
    ];
    
    // Array format
    [
        ['a', 'b', 'c'],
        ['a1', 'b1', 'c1'],
    ];
    
    // Key-Value Pair format (for duplicate headers)
    [
        [
            ['a', 'a1'],
            ['a', 'a2'],
            ['b', 'b1'],
            ['b', 'b2'],
            ['c', 'c1'],
            ['c', 'c2'],
        ],
    ];
  4. Understand parsing performance differences between quoted and non-quoted CSVs

    main

    When evaluating fast-csv performance for your use case, note that CSV files containing quoted columns require additional parsing logic compared to non-quoted files.

    • Quoted CSVs: Require more complex logic to handle escape characters and delimiters within quotes. Benchmarks show higher average processing times per row compared to non-quoted data.
    • Non-quoted CSVs: Require less logic to parse and are generally faster.

    Use these performance characteristics to estimate processing time based on your expected data format and row counts.

  5. Run `@fast-csv/format` TypeScript examples

    main

    The @fast-csv/format TypeScript examples repository provides demonstrations of CSV formatting capabilities. You can manage the examples using the following npm scripts:

    • Build the examples: Compile the TypeScript code.
    • Run all examples: Execute the entire suite of examples.
    • List examples: View a list of all available example names.
    • Run a specific example: Execute a single example by name.
  6. Run @fast-csv/parse TypeScript examples

    main

    This repository contains TypeScript examples for the @fast-csv/parse package. You can build the examples, list available ones, or run them individually using the following npm scripts:

    • Build the examples: npm run build
    • List all available examples: npm run list
    • Run all examples: npm run all-examples
    • Run a specific example: npm run example -- {example_name}
  7. Deploy the fast-csv website to GitHub Pages

    main

    You can build and push the website to the gh-pages branch using the pnpm deploy command. You must provide your GitHub username via the GIT_USER environment variable. If you are using SSH for deployment, set USE_SSH=true.

    $ GIT_USER=<Your GitHub username> USE_SSH=true pnpm deploy