Skip to content

Commit 59af919

Browse files
sapicscodebytere
authored andcommitted
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 d1baae3 commit 59af919

4 files changed

Lines changed: 14 additions & 10 deletions

File tree

‎lib/internal/querystring.js‎

Lines changed: 3 additions & 2 deletions
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(leti=0;i<256;++i)
1112
hexTable[i]='%'+((i<16 ? '0' : '')+i.toString(16)).toUpperCase();
1213

13-
constisHexTable=[
14+
constisHexTable=newInt8Array([
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
functionencodeStr(str,noEscapeTable,hexTable){
3334
constlen=str.length;

‎lib/internal/url.js‎

Lines changed: 3 additions & 2 deletions
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,
@@ -819,7 +820,7 @@ function parseParams(qs) {
819820

820821
// Adapted from querystring's implementation.
821822
// Ref: https://url.spec.whatwg.org/#concept-urlencoded-byte-serializer
822-
constnoEscape=[
823+
constnoEscape=newInt8Array([
823824
/*
824825
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
825826
*/
@@ -831,7 +832,7 @@ const noEscape = [
831832
1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,// 0x50 - 0x5F
832833
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x60 - 0x6F
833834
1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0// 0x70 - 0x7F
834-
];
835+
]);
835836

836837
// Special version of hexTable that uses `+` for U+0020 SPACE.
837838
constparamHexTable=hexTable.slice();

‎lib/querystring.js‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
const{
2727
Array,
2828
ArrayIsArray,
29+
Int8Array,
2930
MathAbs,
3031
NumberIsFinite,
3132
ObjectCreate,
@@ -54,7 +55,7 @@ const QueryString = module.exports = {
5455
decode: parse
5556
};
5657

57-
constunhexTable=[
58+
constunhexTable=newInt8Array([
5859
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,// 0 - 15
5960
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,// 16 - 31
6061
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,// 32 - 47
@@ -71,7 +72,7 @@ const unhexTable = [
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,
7374
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1// ... 255
74-
];
75+
]);
7576
// A safe fast alternative to decodeURIComponent
7677
functionunescapeBuffer(s,decodeSpaces){
7778
constout=Buffer.allocUnsafe(s.length);
@@ -131,7 +132,7 @@ function qsUnescape(s, decodeSpaces) {
131132
// digits
132133
// alpha (uppercase)
133134
// alpha (lowercase)
134-
constnoEscape=[
135+
constnoEscape=newInt8Array([
135136
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,// 0 - 15
136137
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,// 16 - 31
137138
0,1,0,0,0,0,0,1,1,1,1,0,0,1,1,0,// 32 - 47
@@ -140,7 +141,7 @@ const noEscape = [
140141
1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,// 80 - 95
141142
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 96 - 111
142143
1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,0// 112 - 127
143-
];
144+
]);
144145
// QueryString.escape() replaces encodeURIComponent()
145146
// https://www.ecma-international.org/ecma-262/5.1/#sec-15.1.3.4
146147
functionqsEscape(str){

‎lib/url.js‎

Lines changed: 3 additions & 2 deletions
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-
constnoEscapeAuth=[
565+
constnoEscapeAuth=newInt8Array([
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=functionformat(){
576577
letauth=this.auth||'';

0 commit comments

Comments
 (0)