-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincremental-json-parsing.js
132 lines (109 loc) · 2.71 KB
/
incremental-json-parsing.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const b = require("benny");
const fs = require("fs");
function indentString(string, count = 1, options = {}) {
const { indent = " ", includeEmptyLines = false } = options;
if (typeof string !== "string") {
throw new TypeError(
`Expected \`input\` to be a \`string\`, got \`${typeof string}\``
);
}
if (typeof count !== "number") {
throw new TypeError(
`Expected \`count\` to be a \`number\`, got \`${typeof count}\``
);
}
if (count < 0) {
throw new RangeError(
`Expected \`count\` to be at least 0, got \`${count}\``
);
}
if (typeof indent !== "string") {
throw new TypeError(
`Expected \`options.indent\` to be a \`string\`, got \`${typeof indent}\``
);
}
if (count === 0) {
return string;
}
const regex = includeEmptyLines ? /^/gm : /^(?!\s*$)/gm;
return string.replace(regex, indent.repeat(count));
}
function incrementalNoIndent(obj) {
let str = `{
"type": "FeatureCollection",
"features": [`;
for (let i = 0; i < obj.features.length; i++) {
if (i < obj.features.length - 1) str += ",\n";
str += JSON.stringify(obj.features[i], null, 2);
}
return (
str +
` ]
}`
);
}
function incrementalSingleIndent(obj) {
let str = `{
"type": "FeatureCollection",
"features": [`;
let features = "";
for (let i = 0; i < obj.features.length; i++) {
if (i > 0) str += ",\n";
features += JSON.stringify(obj.features[i], null, 2);
}
return (
str +
indentString(features) +
` ]
}`
);
}
function incremental(obj) {
let str = `{
"type": "FeatureCollection",
"features": [`;
for (let i = 0; i < obj.features.length; i++) {
if (i < obj.features.length - 1) str += ",\n";
str += indentString(JSON.stringify(obj.features[i], null, 2), 2);
}
return (
str +
` ]
}`
);
}
b.suite(
"JSON partial stringify",
b.add("Single JSON pass", () => {
const gj = JSON.parse(fs.readFileSync("./example.geojson"));
return () => {
JSON.stringify(gj);
};
}),
b.add("Incremental", () => {
const gj = JSON.parse(fs.readFileSync("./example.geojson"));
return () => {
incremental(gj);
};
}),
b.add("Incremental no indent", () => {
const gj = JSON.parse(fs.readFileSync("./example.geojson"));
return () => {
incrementalNoIndent(gj);
};
}),
b.add("Incremental single indent", () => {
const gj = JSON.parse(fs.readFileSync("./example.geojson"));
return () => {
incrementalSingleIndent(gj);
};
}),
b.add("Stringify single feature", () => {
const gj = JSON.parse(fs.readFileSync("./example.geojson"));
return () => {
indentString(JSON.stringify(gj.features[2]));
};
}),
b.cycle(),
b.complete()
);