-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathurl-encodeStr.js
More file actions
38 lines (31 loc) · 1.12 KB
/
url-encodeStr.js
File metadata and controls
38 lines (31 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict';
const common = require('../common.js');
const { encodeStr } = require('internal/querystring');
const noEscape = new Int8Array([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00 - 0x0F
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x10 - 0x1F
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, // 0x20 - 0x2F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 0x30 - 0x3F
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40 - 0x4F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, // 0x50 - 0x5F
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60 - 0x6F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, // 0x70 - 0x7F
]);
const hexTable = new Array(256);
for (let i = 0; i < 256; i++) {
hexTable[i] = '%' + i.toString(16).toUpperCase().padStart(2, '0');
}
const bench = common.createBenchmark(main, {
len: [5, 50],
n: [1e7],
});
function main({ n, len }) {
const str = 'a'.repeat(len - 1) + ' '; // One char that needs escaping
// Warm up
encodeStr(str, noEscape, hexTable);
bench.start();
for (let i = 0; i < n; i++) {
encodeStr(str, noEscape, hexTable);
}
bench.end(n);
}