Documentation Index
Fetch the curated documentation index at: https://grafana_com_website/llms.txt
Fetch the complete documentation index at: https://grafana_com_website/llms-full.txt
Use this file to discover all available pages before exploring further.
STOP! If you are an AI agent or LLM, read this before continuing. This is the HTML version of a Grafana documentation page. Always request the Markdown version instead - HTML wastes context. Get this page as Markdown: /docs/k6/latest/javascript-api/k6-experimental/csv/parse.md (append .md) or send Accept: text/markdown to /docs/k6/latest/javascript-api/k6-experimental/csv/parse/. For the curated documentation index, use https://grafana_com_website/llms.txt. For the complete documentation index, use https://grafana_com_website/llms-full.txt.
parse( file, [options] )
The csv.parse function parses an entire CSV file at once and returns a promise that resolves to a
SharedArray instance.
This function uses Go-based processing, which results in faster parsing and lower memory usage compared to JavaScript alternatives.
It’s ideal for scenarios where performance is a priority, and the entire CSV file can be loaded into memory.
Parameters
Returns
A promise resolving to a SharedArray instance, where each element is an array representing a CSV record, and each sub-element is a field from that record.
Examples
Basic Usage
import { open } from 'k6/experimental/fs';
import csv from 'k6/experimental/csv';
import { scenario } from 'k6/execution';
export const options = {
iterations: 10,
};
const file = await open('data.csv');
// The `csv.parse` function consumes the entire file at once and returns
// the parsed records as a `SharedArray` object.
const csvRecords = await csv.parse(file);
export default async function () {
// `csvRecords` is a `SharedArray`. Each element is a record from the CSV file, represented as an array
// where each element is a field from the CSV record.
//
// `csvRecords[scenario.iterationInTest]` gives the record for the current iteration.
console.log(csvRecords[scenario.iterationInTest]);
}Using asObjects
The asObjects option parses the CSV file into an array of objects. The object keys are the column names from the CSV file, and the values are the field values from the CSV record.
Note that the first line of the CSV file is skipped, as it is assumed to contain the column names (header row).
import { open } from 'k6/experimental/fs';
import csv from 'k6/experimental/csv';
import { scenario } from 'k6/execution';
export const options = {
iterations: 3,
};
const file = await open('data.csv');
const csvRecords = await csv.parse(file, { asObjects: true });
export default function () {
// Will print the record for the current iteration as an object.
//
// For example, given the following CSV file:
//
// firstname,lastname
// francois,mitterand
// helmut,kohl
// pierre,mendes-france
//
// The test will print:
//
// { firstname: 'francois', lastname: 'mitterand' }
// { firstname: 'helmut', lastname: 'kohl' }
// { firstname: 'pierre', lastname: 'mendes-france' }
console.log(csvRecords[scenario.iterationInTest]);
}Notes on Usage
- Memory considerations:
csv.parseloads the entire CSV file into memory at once, which may lead to increased memory usage and startup time for very large files. - Shared memory usage: The
SharedArray returned by
csv.parseis shared among all Virtual Users (VUs), reducing memory overhead when multiple VUs access the same data.
Was this page helpful?
Related resources from Grafana Labs

