|
| 1 | +import { add, cycle, /* save, */ suite } from 'benny' |
| 2 | +import parse from './parse.js' // 'csv-rex/parse' |
| 3 | + |
| 4 | +const inputs = {} |
| 5 | +const configs = [] |
| 6 | +const baseline = { |
| 7 | + columns: 10, |
| 8 | + rows: 1_000, |
| 9 | + quotes: false, |
| 10 | + newlineChar: '\r\n', |
| 11 | + delimiterChar: ',', |
| 12 | + header: false, |
| 13 | + commentPrefixValue: false |
| 14 | +} |
| 15 | +configs.push({ ...baseline }) |
| 16 | +// expected to be slower, compare against each other |
| 17 | +configs.push({ ...baseline, columns: 100 }) // input has move columns |
| 18 | +configs.push({ ...baseline, rows: 10_000 }) // input has more rows |
| 19 | +// Options |
| 20 | +configs.push({ |
| 21 | + ...baseline, |
| 22 | + header: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] |
| 23 | +}) // pre-defined headers to make object |
| 24 | +configs.push({ ...baseline, header: true }) // use header to make object |
| 25 | +configs.push({ ...baseline, newlineChar: '\n' }) // shorter newline ** should be fastest |
| 26 | +configs.push({ ...baseline, newlineChar: '' }) // detect newline |
| 27 | +configs.push({ ...baseline, delimiterChar: '\t' }) // detect delimiter |
| 28 | +configs.push({ ...baseline, delimiterChar: '' }) // detect delimiter |
| 29 | +configs.push({ ...baseline, commentPrefixValue: '//' }) // detect comments |
| 30 | +configs.push({ ...baseline, quotes: true }) // input has quoted fields |
| 31 | + |
| 32 | +configs.push({ ...baseline, newlineChar: '\n', delimiterChar: '\t' }) // TSV |
| 33 | +configs.push({ ...baseline }) |
| 34 | + |
| 35 | +const baselineDiff = (config) => { |
| 36 | + const diff = {} |
| 37 | + for (const key in config) { |
| 38 | + if (config[key] !== baseline[key]) { |
| 39 | + diff[key] = config[key] |
| 40 | + } |
| 41 | + } |
| 42 | + return diff |
| 43 | +} |
| 44 | + |
| 45 | +const testBatch = (configs) => { |
| 46 | + return configs.map((config) => { |
| 47 | + const { columns, rows, quotes, ...options } = config |
| 48 | + const delimiterChar = options.delimiterChar || baseline.delimiterChar |
| 49 | + const newlineChar = options.newlineChar || baseline.newlineChar |
| 50 | + const input = `${columns}x${rows} w/${ |
| 51 | + quotes ? '' : 'o' |
| 52 | + } quotes and {newlineChar:${newlineChar},delimiterChar:${delimiterChar}}` |
| 53 | + if (!inputs[input]) { |
| 54 | + const wrapper = quotes ? '"' : '' |
| 55 | + const delimiter = quotes ? `"${delimiterChar}"` : `${delimiterChar}` |
| 56 | + let csv = |
| 57 | + wrapper + |
| 58 | + Array.from({ length: columns + 1 }, (_, x) => `__${x}__`).join( |
| 59 | + delimiter |
| 60 | + ) + |
| 61 | + wrapper + |
| 62 | + newlineChar |
| 63 | + for (let y = 0; y < rows; y++) { |
| 64 | + csv += |
| 65 | + wrapper + |
| 66 | + Array.from({ length: columns + 1 }, (_, x) => `${x}x${y}`).join( |
| 67 | + delimiter |
| 68 | + ) + |
| 69 | + wrapper + |
| 70 | + newlineChar |
| 71 | + } |
| 72 | + inputs[input] = csv |
| 73 | + } |
| 74 | + return add( |
| 75 | + `parse(${JSON.stringify({ columns, rows, quotes })}, ${JSON.stringify( |
| 76 | + options |
| 77 | + )}) :: ${JSON.stringify(baselineDiff(config))}`, |
| 78 | + () => { |
| 79 | + parse(inputs[input], options) |
| 80 | + } |
| 81 | + ) |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +const parseSuite = suite( |
| 86 | + 'parse', |
| 87 | + ...testBatch(configs), |
| 88 | + cycle() |
| 89 | + // save({file: 'parse.bench.csv', format: 'csv'}) |
| 90 | +) |
| 91 | + |
| 92 | +export default () => parseSuite |
0 commit comments