<script>
</script>
line = '1,,"""Jillian","Pire"",""li",jpirelli0@seattletimes.com,"Gender,fluid","","93,164,220,186"';
const delimiter = ',';
const wrapper = '"';
const tokens = line.split(delimiter);
const cells = [];
let inWrapper = false;
let tmp = '';
for (let token of tokens) {
if (inWrapper) {
token = `${tmp}${delimiter}${token}`;
tmp = '';
}
const sanitizedToken = token.replace(/"{2}/g, '');
if (sanitizedToken[0] === wrapper) {
inWrapper = true;
token = token.slice(1);
}
if (sanitizedToken.slice(-1) === wrapper) {
inWrapper = false;
token = token.slice(0, -1);
}
if(inWrapper) {
tmp = token;
} else {
cells.push(token.replace(/"{2}/g, '"'));
}
}
let delimiter = ',';
let wrapper = '"';
let cells = [];
let tmp = '';
let inWrapper = false;
let previousChar = '';
for (let i = 0; i < line.length; i++) {
if (line[i] === wrapper && line[i] === line[i+1]) {
tmp += wrapper
i+=2;
}
let char = line[i];
let nextChar = line[i+1];
if((!previousChar || previousChar === delimiter) && char === wrapper) {
inWrapper = true;
continue;
}
if((!nextChar || nextChar === delimiter) && char === wrapper) {
inWrapper = false;
continue;
}
if(!inWrapper && char === delimiter) {
cells.push(tmp);
tmp = '';
} else {
tmp += char
}
previousChar = char;
}
cells.push(tmp);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
split | |
loop |
Test name | Executions per second |
---|---|
split | 461120.5 Ops/sec |
loop | 41560.1 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Definition
The benchmark is defined by two test cases: split
and loop
. Both tests aim to parse a single CSV line, which contains commas (,
) as delimiters. The goal is to split this line into individual cells while handling quoted strings ("..."
).
Options Compared
Two approaches are compared:
split()
function with a delimiter parameter, which splits the input string into an array of substrings using the specified delimiter.Pros and Cons
Split (Method 1)
Pros:
split()
)Cons:
split()
function.Loop (Method 2)
Pros:
Cons:
split()
function.Other Considerations
Both methods have their trade-offs. The split()
method is concise but may not handle quoted strings correctly, while the loop method is more complex but ensures accurate parsing of quoted strings and can be optimized for performance.
In general, when working with CSV data in JavaScript, it's essential to consider how quoted strings are handled, as they can significantly impact the performance and accuracy of your implementation.
Library/Functionality Used
There is no external library or functionality used in this benchmark. The split()
function is a built-in JavaScript method.
Special JS Features/Syntax (None)
No special JavaScript features or syntax are used in this benchmark.
Now, let's discuss alternatives to the two methods compared:
csv-parser
or papaparse
. These libraries can provide more accurate and efficient parsing of CSV data.Overall, the choice between the split()
method and the loop method depends on your specific requirements and priorities, such as performance, accuracy, or code readability.