Skip to content

Commit 6582f12

Browse files
committed
feat: add jsonc-parser to benchmark and update performance metrics
1 parent 578b5ab commit 6582f12

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

benchmark.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const chalk = require('chalk');
44
const fastJsonFormat = require('./src/index.js');
55
const JSONbig = require('json-bigint');
66
const LosslessJSON = require('lossless-json');
7+
const jsoncParser = require('jsonc-parser');
78

89
/**
910
* Generates a nested JSON string of approximately the target size
@@ -76,7 +77,7 @@ const testSizes = [
7677
];
7778

7879
console.log('\n' + chalk.bold.cyan('🚀 Fast JSON Format Benchmark') + '\n');
79-
console.log(chalk.gray('⚡ Comparing ') + chalk.yellow('fast-json-format') + chalk.gray(' vs ') + chalk.yellow('JSON.stringify(JSON.parse())') + chalk.gray(' vs ') + chalk.yellow('json-bigint') + chalk.gray(' vs ') + chalk.yellow('lossless-json') + '\n');
80+
console.log(chalk.gray('⚡ Comparing ') + chalk.yellow('fast-json-format') + chalk.gray(' vs ') + chalk.yellow('jsonc-parser') + chalk.gray(' vs ') + chalk.yellow('json-bigint') + chalk.gray(' vs ') + chalk.yellow('lossless-json') + chalk.gray(' vs ') + chalk.yellow('JSON.stringify(JSON.parse())') + '\n');
8081
console.log(chalk.bold.blue('📊 Generating test data...') + '\n');
8182

8283
const testCases = testSizes.map(size => {
@@ -108,6 +109,9 @@ testCases.forEach(testCase => {
108109
.add('fast-json-format', function() {
109110
fastJsonFormat(testCase.data, ' ');
110111
})
112+
.add('jsonc-parser', function() {
113+
JSON.stringify(jsoncParser.parse(testCase.data), null, 2);
114+
})
111115
.add('json-bigint', function() {
112116
JSONbig.stringify(JSONbig.parse(testCase.data), null, 2);
113117
})
@@ -124,8 +128,8 @@ testCases.forEach(testCase => {
124128

125129
results.push({ name, hz: event.target.hz });
126130

127-
const symbol = results.length === 1 ? '├─' : results.length === 2 ? '├─' : '└─';
128-
const color = name === 'fast-json-format' ? chalk.green : name === 'JSON.stringify' ? chalk.blue : name === 'json-bigint' ? chalk.magenta : chalk.yellow;
131+
const symbol = results.length === 1 ? '├─' : results.length === 2 ? '├─' : results.length === 3 ? '├─' : results.length === 4 ? '├─' : '└─';
132+
const color = name === 'fast-json-format' ? chalk.green : name === 'JSON.stringify' ? chalk.blue : name === 'json-bigint' ? chalk.magenta : name === 'jsonc-parser' ? chalk.cyan : chalk.yellow;
129133

130134
console.log(chalk.gray(` ${symbol} `) + color(name) + chalk.gray(': ') + chalk.bold.white(ops) + chalk.gray(' ops/sec ±' + margin + '%'));
131135
})
@@ -147,34 +151,38 @@ testCases.forEach(testCase => {
147151
console.log('\n\n' + chalk.bold.cyan('📊 Summary Table') + '\n');
148152

149153
// Build table header
150-
const libs = ['fast-json-format', 'json-bigint', 'lossless-json', 'JSON.stringify'];
154+
const libs = ['fast-json-format', 'jsonc-parser', 'json-bigint', 'lossless-json', 'JSON.stringify'];
151155
const colWidths = { size: 12, lib: 20 };
152156

153157
// Header
154158
console.log(
155159
chalk.bold.white('Size'.padEnd(colWidths.size)) + ' │ ' +
156160
chalk.bold.green('fast-json-format'.padEnd(colWidths.lib)) + ' │ ' +
161+
chalk.bold.cyan('jsonc-parser'.padEnd(colWidths.lib)) + ' │ ' +
157162
chalk.bold.magenta('json-bigint'.padEnd(colWidths.lib)) + ' │ ' +
158163
chalk.bold.yellow('lossless-json'.padEnd(colWidths.lib)) + ' │ ' +
159164
chalk.bold.blue('JSON.stringify'.padEnd(colWidths.lib))
160165
);
161-
console.log('─'.repeat(colWidths.size) + '─┼─' + '─'.repeat(colWidths.lib) + '─┼─' + '─'.repeat(colWidths.lib) + '─┼─' + '─'.repeat(colWidths.lib) + '─┼─' + '─'.repeat(colWidths.lib));
166+
console.log('─'.repeat(colWidths.size) + '─┼─' + '─'.repeat(colWidths.lib) + '─┼─' + '─'.repeat(colWidths.lib) + '─┼─' + '─'.repeat(colWidths.lib) + '─┼─' + '─'.repeat(colWidths.lib) + '─┼─' + '─'.repeat(colWidths.lib));
162167

163168
// Rows
164169
allResults.forEach(result => {
165170
const fastJson = result.results.find(r => r.name === 'fast-json-format');
166171
const jsonBigint = result.results.find(r => r.name === 'json-bigint');
167172
const losslessJson = result.results.find(r => r.name === 'lossless-json');
173+
const jsoncParser = result.results.find(r => r.name === 'jsonc-parser');
168174
const jsonStringify = result.results.find(r => r.name === 'JSON.stringify');
169175

170176
const fastJsonOps = fastJson ? fastJson.hz.toFixed(0) : 'N/A';
171177
const jsonBigintOps = jsonBigint ? jsonBigint.hz.toFixed(0) : 'N/A';
172178
const losslessJsonOps = losslessJson ? losslessJson.hz.toFixed(0) : 'N/A';
179+
const jsoncParserOps = jsoncParser ? jsoncParser.hz.toFixed(0) : 'N/A';
173180
const jsonStringifyOps = jsonStringify ? jsonStringify.hz.toFixed(0) : 'N/A';
174181

175182
console.log(
176183
chalk.cyan(result.size.padEnd(colWidths.size)) + ' │ ' +
177184
chalk.white((fastJsonOps + ' ops/sec').padEnd(colWidths.lib)) + ' │ ' +
185+
chalk.white((jsoncParserOps + ' ops/sec').padEnd(colWidths.lib)) + ' │ ' +
178186
chalk.white((jsonBigintOps + ' ops/sec').padEnd(colWidths.lib)) + ' │ ' +
179187
chalk.white((losslessJsonOps + ' ops/sec').padEnd(colWidths.lib)) + ' │ ' +
180188
chalk.white((jsonStringifyOps + ' ops/sec').padEnd(colWidths.lib))

package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"chalk": "^4.1.2",
2727
"jest": "^30.2.0",
2828
"json-bigint": "^1.0.0",
29+
"jsonc-parser": "^3.3.1",
2930
"lossless-json": "^4.3.0"
3031
},
3132
"license": "MIT"

readme.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ JSON.stringify is inherently faster (as it’s native and C++-optimized)
6464
Performance improvements are welcome :)
6565

6666
```text
67-
Size │ fast-json-format │ json-bigint │ lossless-json │ JSON.stringify
68-
─────────────┼──────────────────────┼──────────────────────┼──────────────────────┼─────────────────────
69-
100 KB │ 1064 ops/sec │ 712 ops/sec │ 609 ops/sec │ 2432 ops/sec
70-
1 MB │ 91 ops/sec │ 65 ops/sec │ 43 ops/sec │ 238 ops/sec
71-
5 MB │ 15 ops/sec │ 13 ops/sec │ 6 ops/sec │ 47 ops/sec
72-
10 MB │ 7 ops/sec │ 7 ops/sec │ 3 ops/sec │ 23 ops/sec
67+
Size │ fast-json-format │ jsonc-parser │ json-bigint │ lossless-json │ JSON.stringify
68+
─────────────┼──────────────────────┼──────────────────────┼──────────────────────┼──────────────────────┼─────────────────────
69+
100 KB │ 1839 ops/sec │ 1265 ops/sec │ 1053 ops/sec │ 886 ops/sec │ 3025 ops/sec
70+
1 MB │ 178 ops/sec │ 125 ops/sec │ 98 ops/sec │ 61 ops/sec │ 296 ops/sec
71+
5 MB │ 28 ops/sec │ 21 ops/sec │ 18 ops/sec │ 9 ops/sec │ 58 ops/sec
72+
10 MB │ 15 ops/sec │ 11 ops/sec │ 9 ops/sec │ 4 ops/sec │ 30 ops/sec
7373
```
7474

7575
## Testing

0 commit comments

Comments
 (0)