Skip to content

Commit 4f92188

Browse files
committedOct 24, 2023
chore: update benchmarks
1 parent 4dbb707 commit 4f92188

File tree

3 files changed

+59
-50
lines changed

3 files changed

+59
-50
lines changed
 

‎BENCH.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
=== Non-string fallback ==
2+
JSON.parse x 9,795,965 ops/sec ±1.05% (99 runs sampled)
3+
destr x 251,242,150 ops/sec ±0.16% (97 runs sampled)
4+
safeDestr x 71,403,846 ops/sec ±0.30% (98 runs sampled)
5+
sjson:
6+
@hapi/bourne x 9,398,223 ops/sec ±0.20% (94 runs sampled)
7+
Fastest is destr
8+
9+
=== Known values ==
10+
JSON.parse x 24,819,945 ops/sec ±0.67% (98 runs sampled)
11+
destr x 64,177,771 ops/sec ±0.23% (98 runs sampled)
12+
safeDestr x 35,102,427 ops/sec ±0.50% (95 runs sampled)
13+
sjson x 13,214,663 ops/sec ±1.16% (96 runs sampled)
14+
@hapi/bourne x 22,661,470 ops/sec ±0.38% (100 runs sampled)
15+
Fastest is destr
16+
17+
=== plain string (short) ==
18+
JSON.parse (try-catch) x 13,619,068 ops/sec ±0.96% (95 runs sampled)
19+
destr x 69,967,512 ops/sec ±0.45% (95 runs sampled)
20+
safeDestr x 36,915,513 ops/sec ±2.45% (92 runs sampled)
21+
sjson (try-catch) x 9,223,733 ops/sec ±0.31% (98 runs sampled)
22+
@hapi/bourne x 12,190,344 ops/sec ±0.80% (94 runs sampled)
23+
Fastest is destr
24+
25+
=== plain string (long) ==
26+
JSON.parse (try-catch) x 153,349 ops/sec ±0.55% (99 runs sampled)
27+
destr x 36,427,741 ops/sec ±0.34% (97 runs sampled)
28+
safeDestr:
29+
sjson (try-catch) x 325,713 ops/sec ±0.88% (93 runs sampled)
30+
@hapi/bourne:
31+
Fastest is destr
32+
33+
=== package.json ==
34+
JSON.parse x 324,505 ops/sec ±0.12% (94 runs sampled)
35+
destr x 274,520 ops/sec ±0.15% (97 runs sampled)
36+
safeDestr x 271,451 ops/sec ±0.16% (95 runs sampled)
37+
sjson x 264,515 ops/sec ±1.73% (93 runs sampled)
38+
@hapi/bourne x 294,835 ops/sec ±0.19% (97 runs sampled)
39+
Fastest is JSON.parse
40+
41+
=== broken object ==
42+
JSON.parse (try-catch) x 99,745 ops/sec ±0.53% (96 runs sampled)
43+
destr x 86,757 ops/sec ±0.55% (98 runs sampled)
44+
safeDestr:
45+
sjson (try-catch) x 154,320 ops/sec ±0.37% (97 runs sampled)
46+
@hapi/bourne:
47+
Fastest is sjson (try-catch)

‎README.md

+2-44
Original file line numberDiff line numberDiff line change
@@ -118,51 +118,9 @@ safeDestr("[foo");
118118

119119
## Benchmarks
120120

121-
Locally try with `pnpm benchmark`. Below are results on Node.js **v20.5.0** with MBA M2.
121+
`destr` is sometimes little bit slower than `JSON.parse` when parsing a valid JSON string mainly because of transform to avoid [prototype pollution](https://learn.snyk.io/lessons/prototype-pollution/javascript/) which can lead to serious security issues if not being sanitized. In the other words, `destr` is better when input is not always a JSON string or from untrusted source like request body.
122122

123-
**Note** `destr` is sometimes little bit slower than `JSON.parse` when parsing a valid JSON string mainly because of transform to avoid [prototype pollution](https://learn.snyk.io/lessons/prototype-pollution/javascript/) which can lead to serious security issues if not being sanitized. In the other words, `destr` is better when input is not always a JSON string or from untrusted source like request body.
124-
125-
```
126-
=== Non-string fallback ==
127-
JSON.parse x 9,506,702 ops/sec ±1.68% (92 runs sampled)
128-
destr x 159,565,446 ops/sec ±0.24% (98 runs sampled)
129-
safeDestr x 73,624,106 ops/sec ±0.39% (95 runs sampled)
130-
sjson:
131-
@hapi/bourne x 9,404,463 ops/sec ±0.62% (96 runs sampled)
132-
Fastest is destr
133-
134-
=== Known values ==
135-
JSON.parse x 15,000,474 ops/sec ±0.82% (93 runs sampled)
136-
destr x 96,977,026 ops/sec ±0.13% (101 runs sampled)
137-
safeDestr x 47,618,310 ops/sec ±0.15% (98 runs sampled)
138-
sjson x 11,176,069 ops/sec ±0.47% (93 runs sampled)
139-
@hapi/bourne x 14,650,782 ops/sec ±0.64% (93 runs sampled)
140-
Fastest is destr
141-
142-
=== plain string ==
143-
JSON.parse (try-catch) x 11,775,641 ops/sec ±0.64% (95 runs sampled)
144-
destr x 111,118,106 ops/sec ±0.53% (101 runs sampled)
145-
safeDestr x 52,455,654 ops/sec ±0.21% (97 runs sampled)
146-
sjson (try-catch) x 9,282,956 ops/sec ±0.47% (96 runs sampled)
147-
@hapi/bourne x 11,547,144 ops/sec ±0.68% (96 runs sampled)
148-
Fastest is destr
149-
150-
=== package.json ==
151-
JSON.parse x 420,496 ops/sec ±0.20% (98 runs sampled)
152-
destr x 358,257 ops/sec ±0.58% (98 runs sampled)
153-
safeDestr x 351,278 ops/sec ±1.10% (98 runs sampled)
154-
sjson x 358,003 ops/sec ±0.14% (102 runs sampled)
155-
@hapi/bourne x 398,852 ops/sec ±0.32% (99 runs sampled)
156-
Fastest is JSON.parse
157-
158-
=== broken object ==
159-
JSON.parse (try-catch) x 137,788 ops/sec ±0.68% (98 runs sampled)
160-
destr x 111,878 ops/sec ±0.50% (98 runs sampled)
161-
safeDestr:
162-
sjson (try-catch) x 219,924 ops/sec ±0.62% (98 runs sampled)
163-
@hapi/bourne:
164-
Fastest is sjson (try-catch)
165-
```
123+
Check [Benchmarks](./BENCH.md)
166124

167125
## License
168126

‎bench.mjs

+10-6
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function benchTryCatch(name, val) {
4545
suite.add("JSON.parse (try-catch)", () => {
4646
try {
4747
JSON.parse(val);
48-
} catch (err) {
48+
} catch {
4949
return val;
5050
}
5151
});
@@ -58,7 +58,7 @@ function benchTryCatch(name, val) {
5858
suite.add("sjson (try-catch)", () => {
5959
try {
6060
sjson.parse(val);
61-
} catch (err) {
61+
} catch {
6262
return val;
6363
}
6464
});
@@ -68,10 +68,14 @@ function benchTryCatch(name, val) {
6868
suite.run();
6969
}
7070

71-
bench("Non-string fallback", 3.14159265359);
71+
bench("Non-string fallback", 3.141_592_653_59);
7272
bench("Known values", "true");
73-
benchTryCatch("plain string", `"SALAM"`);
7473

75-
const pkg = fs.readFileSync("./package.json", "utf-8");
74+
benchTryCatch("plain string (short)", `"SALAM"`);
75+
76+
const longStr = fs.readFileSync("./pnpm-lock.yaml", "utf8");
77+
benchTryCatch("plain string (long)", longStr);
78+
79+
const pkg = fs.readFileSync("./package.json", "utf8");
7680
bench("package.json", pkg);
77-
benchTryCatch("broken object", pkg.substring(0, pkg.length - 1));
81+
benchTryCatch("broken object", pkg.slice(0, Math.max(0, pkg.length - 1)));

0 commit comments

Comments
 (0)
Please sign in to comment.