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