forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-buffer-parser.js
More file actions
46 lines (38 loc) · 1006 Bytes
/
json-buffer-parser.js
File metadata and controls
46 lines (38 loc) · 1006 Bytes
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
'use strict';
const { readFile } = require('node:fs/promises');
const common = require('../common.js');
const { parseFromBuffer } = require('node:json');
const path = require('node:path');
const assert = require('node:assert');
const configs = {
n: [10, 1024],
parser: ['native', 'node'],
input: [
'simple-string',
'simple-number-array',
'simple-string-array',
'simple-object',
'nested-ascii',
'nested-unicode',
'twitter',
],
};
const bench = common.createBenchmark(main, configs);
async function main(conf) {
function parseNative(buf) {
return JSON.parse(buf.toString());
}
function parseNode(buf) {
return parseFromBuffer(buf);
}
const fn = conf.parser === 'native' ? parseNative : parseNode;
const filename = `${conf.input}.json`;
const json = await readFile(path.join(__dirname, filename));
let deadcode;
bench.start();
for (let i = 0; i < conf.n; i++) {
deadcode = fn(json);
}
bench.end(conf.n);
assert.ok(deadcode);
}