Skip to content

Commit 6b5242d

Browse files
committed
perf: improve equality detection and reuse it
1 parent 6bf2636 commit 6b5242d

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ console.log(qs.stringify({ foo: ['bar', 'baz'] }))
5252
╔═════════════════════════════════════════╤═════════╤═══════════════════╤═══════════╗
5353
║ Slower tests │ Samples │ Result │ Tolerance ║
5454
╟─────────────────────────────────────────┼─────────┼───────────────────┼───────────╢
55-
║ qs │ 10000 │ 302595.31 op/sec │ ± 1.22 % ║
56-
║ query-string │ 9500334820.82 op/sec │ ± 0.99 % ║
57-
║ querystringify │ 1000 437899.50 op/sec │ ± 0.73 % ║
58-
║ @aws-sdk/querystring-parser │ 1000 │ 454836.96 op/sec │ ± 0.69 % ║
59-
║ URLSearchParams-with-Object.fromEntries │ 1500 849572.92 op/sec │ ± 0.89 % ║
60-
║ URLSearchParams-with-construct │ 10000 │ 1190835.28 op/sec │ ± 3.22 % ║
61-
║ node:querystring │ 10000 │ 1384717.43 op/sec │ ± 2.99 % ║
62-
║ querystringparser │ 35001735544.65 op/sec │ ± 0.95 % ║
55+
║ qs │ 10000 │ 310825.09 op/sec │ ± 1.29 % ║
56+
║ query-string │ 1000340059.83 op/sec │ ± 0.82 % ║
57+
║ querystringify │ 10000 435456.34 op/sec │ ± 1.06 % ║
58+
║ @aws-sdk/querystring-parser │ 1000 │ 451618.35 op/sec │ ± 0.85 % ║
59+
║ URLSearchParams-with-Object.fromEntries │ 10000 876030.86 op/sec │ ± 1.78 % ║
60+
║ URLSearchParams-with-construct │ 10000 │ 1239366.24 op/sec │ ± 2.59 % ║
61+
║ node:querystring │ 10000 │ 1442731.43 op/sec │ ± 2.95 % ║
62+
║ querystringparser │ 30001863385.29 op/sec │ ± 0.99 % ║
6363
╟─────────────────────────────────────────┼─────────┼───────────────────┼───────────╢
6464
║ Fastest test │ Samples │ Result │ Tolerance ║
6565
╟─────────────────────────────────────────┼─────────┼───────────────────┼───────────╢
66-
║ fast-querystring │ 10000 │ 2023187.35 op/sec │ ± 2.67 % ║
66+
║ fast-querystring │ 10000 │ 2086260.18 op/sec │ ± 3.18 % ║
6767
╚═════════════════════════════════════════╧═════════╧═══════════════════╧═══════════╝
6868
```
6969

lib/parse.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,22 @@ function parse(input) {
2929
let keyHasPlus = false;
3030
let valueHasPlus = false;
3131
let hasBothKeyValuePair = false;
32-
let keyEndingIndex = 0;
32+
let c = 0;
3333

3434
// Have a boundary of input.length + 1 to access last pair inside the loop.
3535
for (let i = 0; i < inputLength + 1; i++) {
36-
let c = i !== inputLength ? input.charCodeAt(i) : -1;
36+
c = i !== inputLength ? input.charCodeAt(i) : 38;
3737

3838
// Handle '&' and end of line to pass the current values to result
39-
if (c === 38 || c === -1) {
39+
if (c === 38) {
4040
hasBothKeyValuePair = equalityIndex > startingIndex;
41-
keyEndingIndex = hasBothKeyValuePair ? equalityIndex : i;
42-
key = input.slice(startingIndex + 1, keyEndingIndex);
41+
42+
// Optimization: Reuse equality index to store the end of key
43+
if (!hasBothKeyValuePair) {
44+
equalityIndex = i;
45+
}
46+
47+
key = input.slice(startingIndex + 1, equalityIndex);
4348

4449
// Add key/value pair only if the range size is greater than 1; a.k.a. contains at least "="
4550
if (hasBothKeyValuePair || key.length > 0) {

0 commit comments

Comments
 (0)