HTML Preparation code:
x
 
1
2
<script>
3
4
5
6
</script>
Script Preparation code:
 
line = '1,,"""Jillian","Pire"",""li",jpirelli0@seattletimes.com,"Gender,fluid","","93,164,220,186"';
Tests:
  • split

     
        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, '"'));
            }
        }
        
  • loop

     
    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);
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    split
    loop

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 3 years ago)
Mozilla/5.0 (iPhone; CPU iPhone OS 14_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Mobile/15E148 Safari/604.1
Mobile Safari 14 on iOS 14.4
View result in a separate tab
Test name Executions per second
split 461120.5 Ops/sec
loop 41560.1 Ops/sec