Skip to content

Commit e161249

Browse files
sapicsBethGriggs
authored andcommittedDec 15, 2020
querystring: reduce memory usage by Int8Array
PR-URL: #34179 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent b05cdfe commit e161249

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed
 

‎lib/internal/querystring.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
Array,
5+
Int8Array,
56
} = primordials;
67

78
const { ERR_INVALID_URI } = require('internal/errors').codes;
@@ -10,7 +11,7 @@ const hexTable = new Array(256);
1011
for (let i = 0; i < 256; ++i)
1112
hexTable[i] = '%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase();
1213

13-
const isHexTable = [
14+
const isHexTable = new Int8Array([
1415
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
1516
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
1617
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 32 - 47
@@ -27,7 +28,7 @@ const isHexTable = [
2728
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2829
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2930
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // ... 256
30-
];
31+
]);
3132

3233
function encodeStr(str, noEscapeTable, hexTable) {
3334
const len = str.length;

‎lib/internal/url.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
Array,
5+
Int8Array,
56
Number,
67
ObjectCreate,
78
ObjectDefineProperties,
@@ -817,7 +818,7 @@ function parseParams(qs) {
817818

818819
// Adapted from querystring's implementation.
819820
// Ref: https://url.spec.whatwg.org/#concept-urlencoded-byte-serializer
820-
const noEscape = [
821+
const noEscape = new Int8Array([
821822
/*
822823
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
823824
*/
@@ -829,7 +830,7 @@ const noEscape = [
829830
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, // 0x50 - 0x5F
830831
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60 - 0x6F
831832
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 // 0x70 - 0x7F
832-
];
833+
]);
833834

834835
// Special version of hexTable that uses `+` for U+0020 SPACE.
835836
const paramHexTable = hexTable.slice();

‎lib/querystring.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
const {
2727
Array,
2828
ArrayIsArray,
29+
Int8Array,
2930
MathAbs,
3031
ObjectCreate,
3132
ObjectKeys,
@@ -53,7 +54,7 @@ const QueryString = module.exports = {
5354
decode: parse
5455
};
5556

56-
const unhexTable = [
57+
const unhexTable = new Int8Array([
5758
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0 - 15
5859
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16 - 31
5960
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 32 - 47
@@ -70,7 +71,7 @@ const unhexTable = [
7071
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
7172
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
7273
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 // ... 255
73-
];
74+
]);
7475
// A safe fast alternative to decodeURIComponent
7576
function unescapeBuffer(s, decodeSpaces) {
7677
const out = Buffer.allocUnsafe(s.length);
@@ -130,7 +131,7 @@ function qsUnescape(s, decodeSpaces) {
130131
// digits
131132
// alpha (uppercase)
132133
// alpha (lowercase)
133-
const noEscape = [
134+
const noEscape = new Int8Array([
134135
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
135136
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
136137
0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, // 32 - 47
@@ -139,7 +140,7 @@ const noEscape = [
139140
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, // 80 - 95
140141
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
141142
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0 // 112 - 127
142-
];
143+
]);
143144
// QueryString.escape() replaces encodeURIComponent()
144145
// https://www.ecma-international.org/ecma-262/5.1/#sec-15.1.3.4
145146
function qsEscape(str) {

‎lib/url.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'use strict';
2323

2424
const {
25+
Int8Array,
2526
ObjectCreate,
2627
ObjectKeys,
2728
SafeSet,
@@ -561,7 +562,7 @@ function urlFormat(urlObject, options) {
561562
// digits
562563
// alpha (uppercase)
563564
// alpha (lowercase)
564-
const noEscapeAuth = [
565+
const noEscapeAuth = new Int8Array([
565566
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00 - 0x0F
566567
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x10 - 0x1F
567568
0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, // 0x20 - 0x2F
@@ -570,7 +571,7 @@ const noEscapeAuth = [
570571
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, // 0x50 - 0x5F
571572
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60 - 0x6F
572573
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0 // 0x70 - 0x7F
573-
];
574+
]);
574575

575576
Url.prototype.format = function format() {
576577
let auth = this.auth || '';

0 commit comments

Comments
 (0)
Please sign in to comment.