Skip to content

Commit d85f188

Browse files
committed
benchmark: create buffer parse benchmark
1 parent d4f4bd5 commit d85f188

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
const { readFile } = require('node:fs/promises');
3+
const common = require('../common.js');
4+
const { parseFromBuffer } = require('node:json');
5+
const path = require('node:path');
6+
const assert = require('node:assert');
7+
8+
const configs = {
9+
n: [1024],
10+
parser: ['native', 'node'],
11+
input: ['simple-array', 'simple-object', 'nested-ascii', 'nested-unicode', 'twitter'],
12+
};
13+
14+
const bench = common.createBenchmark(main, configs);
15+
16+
async function main(conf) {
17+
function parseNative(buf) {
18+
return JSON.parse(buf.toString());
19+
}
20+
21+
function parseNode(buf) {
22+
return parseFromBuffer(buf);
23+
}
24+
25+
const fn = conf.parser === 'native' ? parseNative : parseNode;
26+
27+
const filename = `${conf.input}.json`;
28+
const json = await readFile(path.join(__dirname, filename));
29+
30+
let deadcode;
31+
bench.start();
32+
for (let i = 0; i < conf.n; i++) {
33+
deadcode = fn(json);
34+
}
35+
bench.end(conf.n);
36+
37+
assert.ok(deadcode);
38+
}

0 commit comments

Comments
 (0)