Skip to content

Commit 7e37628

Browse files
TimothyGuitaloacasas
authored andcommitted
benchmark: add url/url-searchparams-sort.js
Backport-of: #11098
1 parent c25c16c commit 7e37628

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const URLSearchParams = require('url').URLSearchParams;
4+
5+
const inputs = {
6+
empty: '',
7+
sorted: 'a&b&c&d&e&f&g&h&i&j&k&l&m&n&o&p&q&r&s&t&u&v&w&x&y&z',
8+
almostsorted: 'a&b&c&d&e&f&g&i&h&j&k&l&m&n&o&p&q&r&s&t&u&w&v&x&y&z',
9+
reversed: 'z&y&x&w&v&u&t&s&r&q&p&o&n&m&l&k&j&i&h&g&f&e&d&c&b&a',
10+
random: 'm&t&d&c&z&v&a&n&p&y&u&o&h&l&f&j&e&q&b&i&s&x&k&w&r&g',
11+
// 8 parameters
12+
short: 'm&t&d&c&z&v&a&n',
13+
// 88 parameters
14+
long: 'g&r&t&h&s&r&d&w&b&n&h&k&x&m&k&h&o&e&x&c&c&g&e&b&p&p&s&n&j&b&y&z&' +
15+
'u&l&o&r&w&a&u&l&m&f&j&q&p&f&e&y&e&n&e&l&m&w&u&w&t&n&t&q&v&y&c&o&' +
16+
'k&f&j&i&l&m&g&j&d&i&z&q&p&x&q&q&d&n&y&w&g&i&v&r'
17+
};
18+
19+
function getParams(str) {
20+
const out = [];
21+
for (const key of str.split('&')) {
22+
out.push(key, '');
23+
}
24+
return out;
25+
}
26+
27+
const bench = common.createBenchmark(main, {
28+
type: Object.keys(inputs),
29+
n: [1e6]
30+
}, {
31+
flags: ['--expose-internals']
32+
});
33+
34+
function main(conf) {
35+
const searchParams = require('internal/url').searchParamsSymbol;
36+
const input = inputs[conf.type];
37+
const n = conf.n | 0;
38+
const params = new URLSearchParams();
39+
const array = getParams(input);
40+
41+
var i;
42+
bench.start();
43+
for (i = 0; i < n; i++) {
44+
params[searchParams] = array.slice();
45+
params.sort();
46+
}
47+
bench.end(n);
48+
}

lib/internal/url.js

+1
Original file line numberDiff line numberDiff line change
@@ -1215,3 +1215,4 @@ exports.domainToUnicode = domainToUnicode;
12151215
exports.encodeAuth = encodeAuth;
12161216
exports.urlToOptions = urlToOptions;
12171217
exports.formatSymbol = kFormat;
1218+
exports.searchParamsSymbol = searchParams;

0 commit comments

Comments
 (0)