Skip to content

Commit 78dc5c4

Browse files
committed
benchmark: add URLSearchParams scaling and encodeStr fast-path
1 parent cea7fe3 commit 78dc5c4

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
5+
const bench = common.createBenchmark(main, {
6+
length: [5, 500],
7+
encode: ['true', 'false'],
8+
count: [1, 1000],
9+
n: [1e5],
10+
});
11+
12+
function main({ n, length, encode, count }) {
13+
const char = encode === 'true' ? '\u00A5' : 'a';
14+
const key = char.repeat(length);
15+
const value = char.repeat(length);
16+
const params = [];
17+
for (let i = 0; i < count; i++) {
18+
params.push([key, value]);
19+
}
20+
const searchParams = new URLSearchParams(params);
21+
22+
// Warm up
23+
searchParams.toString();
24+
25+
bench.start();
26+
for (let i = 0; i < n; i++) {
27+
searchParams.toString();
28+
}
29+
bench.end(n);
30+
}

benchmark/url/url-encodeStr.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const { encodeStr } = require('internal/querystring');
5+
6+
const noEscape = new Int8Array([
7+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00 - 0x0F
8+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x10 - 0x1F
9+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, // 0x20 - 0x2F
10+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 0x30 - 0x3F
11+
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40 - 0x4F
12+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, // 0x50 - 0x5F
13+
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60 - 0x6F
14+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, // 0x70 - 0x7F
15+
]);
16+
17+
const hexTable = new Array(256);
18+
for (let i = 0; i < 256; i++) {
19+
hexTable[i] = '%' + i.toString(16).toUpperCase().padStart(2, '0');
20+
}
21+
22+
const bench = common.createBenchmark(main, {
23+
len: [5, 50],
24+
n: [1e7],
25+
});
26+
27+
function main({ n, len }) {
28+
const str = 'a'.repeat(len - 1) + ' '; // One char that needs escaping
29+
30+
// Warm up
31+
encodeStr(str, noEscape, hexTable);
32+
33+
bench.start();
34+
for (let i = 0; i < n; i++) {
35+
encodeStr(str, noEscape, hexTable);
36+
}
37+
bench.end(n);
38+
}

0 commit comments

Comments
 (0)