μDSV - A Fast CSV Parser
uDSV (μDSV) is a lightweight and exceptionally fast JavaScript library for parsing CSV strings, adhering mostly to RFC 4180. Currently at version 0.7.3, it maintains an active release cadence with frequent minor updates. A key differentiator is its "Ludicrous Speed™" performance, consistently outperforming popular alternatives like Papa Parse by 2x-5x across various datasets and parsing configurations, ensuring high speed not just on simplified "happy paths." The library is designed for the 99.5% use-case, minimizing complexity and performance trade-offs, making it ideal for robust, high-throughput parsing. It offers both full and incremental parsing, automatic delimiter detection, schema inference with value typing (`string`, `number`, `boolean`, `date`, `json`), and flexible output formats including arrays, objects, and columnar arrays, all within a compact 5KB (minified) footprint and ships with TypeScript types.
Common errors
-
Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source in the following Content Security Policy directive: "script-src 'self'".
cause Your browser environment has a strict Content Security Policy that prohibits the use of `eval` or `new Function()`, which uDSV's typed parsing methods depend on.fixEither adjust your CSP to allow `unsafe-eval` (if your security policy permits), or switch to using `parser.stringArrs()` to get raw string outputs and implement manual type conversion logic in your application. -
CSV parsing error: Unexpected character after quoted field (e.g., '"value"extra' instead of '"value",next').
cause This often occurs when a character immediately follows a closing quote without a delimiter, violating RFC 4180 rules. Or, line breaks within a quoted field do not match the row delimiter.fixVerify that your CSV strictly adheres to RFC 4180. Ensure no extraneous characters exist after quoted fields before a delimiter or newline. Also, confirm that all line breaks inside quoted fields are identical to the file's primary row delimiter.
Warnings
- gotcha Line breaks within quoted CSV values must exactly match the row separator used throughout the rest of the CSV. For example, if rows are separated by `\n`, quoted fields containing line breaks must also use `\n`, not `\r\n` or `\r`.
- gotcha uDSV's advanced `typed*()` methods (e.g., `typedObjs`, `typedArrs`) rely on dynamically-generated functions via `new Function()`. Environments with strict Content Security Policy (CSP) headers that disallow `unsafe-eval` will prevent these methods from functioning.
Install
-
npm install udsv -
yarn add udsv -
pnpm add udsv
Imports
- inferSchema
const { inferSchema } = require('udsv');import { inferSchema } from 'udsv'; - initParser
const { initParser } = require('udsv');import { initParser } from 'udsv'; - parser.stringArrs / parser.typedObjs
const parser = initParser(schema); const stringArrays = parser.stringArrs(csvStr); const typedObjects = parser.typedObjs(stringArrays);
Quickstart
import { inferSchema, initParser } from 'udsv';
const csvString = 'name,age,city\nAlice,30,New York\nBob,24,London\nCharlie,35,"Paris, France"';
// 1. Infer schema from the CSV string
const schema = inferSchema(csvString);
console.log('Inferred Schema:', schema);
// 2. Initialize the parser with the schema
const parser = initParser(schema);
// 3. Parse to arrays of strings (raw values)
const stringArrays = parser.stringArrs(csvString);
console.log('Parsed as String Arrays:', stringArrays);
/* Output: [
['name', 'age', 'city'],
['Alice', '30', 'New York'],
['Bob', '24', 'London'],
['Charlie', '35', 'Paris, France']
] */
// 4. Parse to typed objects (using the inferred schema's types)
const typedObjects = parser.typedObjs(stringArrays);
console.log('Parsed as Typed Objects:', typedObjects);
/* Output: [
{ name: 'Alice', age: 30, city: 'New York' },
{ name: 'Bob', age: 24, city: 'London' },
{ name: 'Charlie', age: 35, city: 'Paris, France' }
] */