GeezerVison is showing github.com — url: keep the next '=' across pairs in parseParams · nodejs/node@447123d
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | +const common = require('../common.js'); | | 3 | +const assert = require('node:assert'); | | 4 | + | | 5 | +const bench = common.createBenchmark(main, { | | 6 | + pair: ['a', 'a=b'], | | 7 | + pairs: [1e3, 1e5], | | 8 | + n: [100], | | 9 | +}); | | 10 | + | | 11 | +function main({ pair, pairs, n }) { | | 12 | + const input = `${pair}&`.repeat(pairs); | | 13 | + let params; | | 14 | + bench.start(); | | 15 | + for (let i = 0; i < n; i++) { | | 16 | + params = new URLSearchParams(input); | | 17 | + } | | 18 | + bench.end(n); | | 19 | + assert.strictEqual(params.size, pairs); | | 20 | +} |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/url.js +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1366,13 +1366,18 @@ function parseParams(qs) { | 1366 | 1366 | const out = []; | 1367 | 1367 | // Native indexOf/slice/push outperform primordials on this tight loop. | 1368 | 1368 | const encoded = qs.indexOf('+', i) !== -1 || qs.indexOf('%', i) !== -1; | | 1369 | + // The next '=' at or after `i`, kept across pairs so that a run of pairs | | 1370 | + // with no '=' does not rescan the rest of the string for each pair. | | 1371 | + let eq = -2; | 1369 | 1372 | while (i < len) { | 1370 | 1373 | let amp = qs.indexOf('&', i); | 1371 | 1374 | if (amp === -1) { | 1372 | 1375 | amp = len; | 1373 | 1376 | } | 1374 | 1377 | if (amp !== i) { | 1375 | | - const eq = qs.indexOf('=', i); | | 1378 | + if (eq !== -1 && eq < i) { | | 1379 | + eq = qs.indexOf('=', i); | | 1380 | + } | 1376 | 1381 | if (eq === -1 || eq > amp) { | 1377 | 1382 | out.push(encoded ? decodeFormComponent(qs, i, amp) : qs.slice(i, amp), ''); | 1378 | 1383 | } else { | |
Collapse file Copy file name to clipboard +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +// Pairs with no '=' mixed with pairs that have one. | | 4 | + | | 5 | +require('../common'); | | 6 | +const assert = require('node:assert'); | | 7 | + | | 8 | +const cases = [ | | 9 | + ['a&b&c=d', [['a', ''], ['b', ''], ['c', 'd']]], | | 10 | + ['a=b&c&d&e=f&g', [['a', 'b'], ['c', ''], ['d', ''], ['e', 'f'], ['g', '']]], | | 11 | + ['a&&b=c=d&&e', [['a', ''], ['b', 'c=d'], ['e', '']]], | | 12 | + ['=&a&=b', [['', ''], ['a', ''], ['', 'b']]], | | 13 | + ['?a&b=', [['a', ''], ['b', '']]], | | 14 | + ['a+b&c%20d=e+f&g', [['a b', ''], ['c d', 'e f'], ['g', '']]], | | 15 | + [`${'a&'.repeat(50)}x=y`, [...Array(50).fill(['a', '']), ['x', 'y']]], | | 16 | +]; | | 17 | + | | 18 | +for (const [input, expected] of cases) { | | 19 | + assert.deepStrictEqual([...new URLSearchParams(input)], expected); | | 20 | +} | | 21 | + | | 22 | +{ | | 23 | + const pairs = 1 << 18; | | 24 | + const params = new URLSearchParams(`${'a&'.repeat(pairs)}x=y`); | | 25 | + assert.strictEqual(params.size, pairs + 1); | | 26 | + assert.strictEqual(params.get('x'), 'y'); | | 27 | +} |
|