GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — url: keep the next '=' across pairs in parseParams · nodejs/node@447123d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • benchmark/url
    • url-searchparams-parse-no-equals.js
  • lib/internal
    • url.js
  • test/parallel
    • test-whatwg-url-searchparams-parse-no-equals.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎benchmark/url/url-searchparams-parse-no-equals.js‎

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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

‎lib/internal/url.js‎

Copy file name to clipboardExpand all lines: lib/internal/url.js
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1366,13 +1366,18 @@ function parseParams(qs) {
13661366
const out = [];
13671367
// Native indexOf/slice/push outperform primordials on this tight loop.
13681368
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;
13691372
while (i < len) {
13701373
let amp = qs.indexOf('&', i);
13711374
if (amp === -1) {
13721375
amp = len;
13731376
}
13741377
if (amp !== i) {
1375-
const eq = qs.indexOf('=', i);
1378+
if (eq !== -1 && eq < i) {
1379+
eq = qs.indexOf('=', i);
1380+
}
13761381
if (eq === -1 || eq > amp) {
13771382
out.push(encoded ? decodeFormComponent(qs, i, amp) : qs.slice(i, amp), '');
13781383
} else {
Collapse file

‎test/parallel/test-whatwg-url-searchparams-parse-no-equals.js‎

Copy file name to clipboard
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.