mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
Reuse precomputed escapes for Latin-1 characters instead of joining two byte escapes on every occurrence. This reduces repeated work when querystring and URLSearchParams serialize these characters. Signed-off-by: Roberto Bianchi <roberto.bianchi@spendesk.com> PR-URL: https://github.com/nodejs/node/pull/66756 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
51 lines
1.6 KiB
JavaScript
51 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const assert = require('node:assert');
|
|
const querystring = require('node:querystring');
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
n: [1e5],
|
|
api: ['querystring', 'URLSearchParams', 'URLSearchParams-new-instance'],
|
|
type: ['ascii', 'latin1', 'mixed', 'twobyte', 'bmp', 'astral'],
|
|
size: [16, 256],
|
|
});
|
|
|
|
function main({ n, api, type, size }) {
|
|
const units = {
|
|
ascii: 'abc/? &=',
|
|
latin1: '\u00e9',
|
|
mixed: 'abcd\u00e9',
|
|
twobyte: '\u03b1',
|
|
bmp: '\u4e2d',
|
|
astral: '\ud83d\ude00',
|
|
};
|
|
const unit = units[type];
|
|
const input = unit.repeat(Math.ceil(size / unit.length)).slice(0, size);
|
|
const params = new URLSearchParams([['value', input]]);
|
|
const expected = api === 'querystring' ? encodeURIComponent(input) :
|
|
`value=${encodeURIComponent(input).replaceAll('%20', '+')}`;
|
|
let actual;
|
|
let checksum = 0;
|
|
bench.start();
|
|
if (api === 'querystring') {
|
|
for (let index = 0; index < n; index++) {
|
|
actual = querystring.escape(input);
|
|
checksum += actual.charCodeAt(actual.length - 1);
|
|
}
|
|
} else if (api === 'URLSearchParams') {
|
|
for (let index = 0; index < n; index++) {
|
|
actual = params.toString();
|
|
checksum += actual.charCodeAt(actual.length - 1);
|
|
}
|
|
} else {
|
|
for (let index = 0; index < n; index++) {
|
|
actual = new URLSearchParams({ value: input }).toString();
|
|
checksum += actual.charCodeAt(actual.length - 1);
|
|
}
|
|
}
|
|
bench.end(n);
|
|
assert.strictEqual(actual, expected);
|
|
assert.strictEqual(checksum, n * expected.charCodeAt(expected.length - 1));
|
|
}
|