Skip to content

Commit 903934e

Browse files
committed
[misc] micro optimization
1 parent 7c8ba00 commit 903934e

File tree

3 files changed

+90
-66
lines changed

3 files changed

+90
-66
lines changed

lib/cmd/decoder/text-decoder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ const readBigIntAsNumberLengthCoded = (packet, opts, throwUnexpectedError) => {
137137
const val = packet._atoi(len);
138138
// We know we're here because either bigIntAsNumber or supportBigNumbers is true
139139
if (opts.supportBigNumbers && opts.bigNumberStrings) {
140-
return String(val);
140+
return `${val}`;
141141
}
142142
return val;
143143
}

lib/cmd/encoder/binary-encoder.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class BinaryEncoder {
174174
const subArrays = [geoBuff];
175175
for (let i = 0; i < coordinateLength; i++) {
176176
const tmpBuf = this.getBufferFromGeometryValue(value.geometries[i]);
177-
if (tmpBuf == null) break;
177+
if (tmpBuf === null) break;
178178
subArrays.push(tmpBuf);
179179
}
180180
geoBuff.writeInt32LE(subArrays.length - 1, 5);
@@ -191,7 +191,7 @@ class BinaryEncoder {
191191
const subArrays = [geoBuff];
192192
for (let i = 0; i < coordinateLength; i++) {
193193
const tmpBuf = this.getBufferFromGeometryValue(value.coordinates[i], type);
194-
if (tmpBuf == null) break;
194+
if (tmpBuf === null) break;
195195
subArrays.push(tmpBuf);
196196
}
197197
geoBuff.writeInt32LE(subArrays.length - 1, 5);

lib/cmd/encoder/text-encoder.js

Lines changed: 87 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@
55

66
const QUOTE = 0x27;
77

8+
// Cache common GeoJSON types
9+
const GEO_TYPES = new Set([
10+
'Point',
11+
'LineString',
12+
'Polygon',
13+
'MultiPoint',
14+
'MultiLineString',
15+
'MultiPolygon',
16+
'GeometryCollection'
17+
]);
18+
19+
// Optimized function to pad numbers with leading zeros
820
const formatDigit = function (val, significantDigit) {
9-
let res = `${val}`;
10-
while (res.length < significantDigit) res = '0' + res;
11-
return res;
21+
const str = `${val}`;
22+
return str.length < significantDigit ? '0'.repeat(significantDigit - str.length) + str : str;
1223
};
1324

1425
class TextEncoder {
@@ -51,27 +62,17 @@ class TextEncoder {
5162
out.writeStringAscii('NULL');
5263
} else TextEncoder.writeParam(out, value[i], opts, info);
5364
}
65+
5466
if (opts.arrayParenthesis) {
5567
out.writeStringAscii(')');
5668
}
5769
} else {
58-
if (
59-
value.type != null &&
60-
[
61-
'Point',
62-
'LineString',
63-
'Polygon',
64-
'MultiPoint',
65-
'MultiLineString',
66-
'MultiPolygon',
67-
'GeometryCollection'
68-
].includes(value.type)
69-
) {
70+
if (value.type != null && GEO_TYPES.has(value.type)) {
7071
//GeoJSON format.
71-
let prefix =
72-
(info.isMariaDB() && info.hasMinVersion(10, 1, 4)) || (!info.isMariaDB() && info.hasMinVersion(5, 7, 6))
73-
? 'ST_'
74-
: '';
72+
const isMariaDb = info.isMariaDB();
73+
const prefix =
74+
(isMariaDb && info.hasMinVersion(10, 1, 4)) || (!isMariaDb && info.hasMinVersion(5, 7, 6)) ? 'ST_' : '';
75+
7576
switch (value.type) {
7677
case 'Point':
7778
out.writeStringAscii(
@@ -136,15 +137,18 @@ class TextEncoder {
136137
} else {
137138
if (opts.permitSetMultiParamEntries) {
138139
let first = true;
139-
for (let key in value) {
140+
for (const key in value) {
140141
const val = value[key];
141142
if (typeof val === 'function') continue;
143+
142144
if (first) {
143145
first = false;
144146
} else {
145147
out.writeStringAscii(',');
146148
}
149+
147150
out.writeString('`' + key + '`');
151+
148152
if (val == null) {
149153
out.writeStringAscii('=NULL');
150154
} else {
@@ -164,33 +168,38 @@ class TextEncoder {
164168

165169
static geometricCollectionToString(geo) {
166170
if (!geo) return '';
171+
172+
const len = geo.length;
167173
let st = '';
168-
for (let i = 0; i < geo.length; i++) {
174+
175+
for (let i = 0; i < len; i++) {
176+
const item = geo[i];
169177
//GeoJSON format.
170-
st += i !== 0 ? ',' : '';
171-
switch (geo[i].type) {
178+
if (i !== 0) st += ',';
179+
180+
switch (item.type) {
172181
case 'Point':
173-
st += `POINT(${TextEncoder.geoPointToString(geo[i].coordinates)})`;
182+
st += `POINT(${TextEncoder.geoPointToString(item.coordinates)})`;
174183
break;
175184

176185
case 'LineString':
177-
st += `LINESTRING(${TextEncoder.geoArrayPointToString(geo[i].coordinates)})`;
186+
st += `LINESTRING(${TextEncoder.geoArrayPointToString(item.coordinates)})`;
178187
break;
179188

180189
case 'Polygon':
181-
st += `POLYGON(${TextEncoder.geoMultiArrayPointToString(geo[i].coordinates)})`;
190+
st += `POLYGON(${TextEncoder.geoMultiArrayPointToString(item.coordinates)})`;
182191
break;
183192

184193
case 'MultiPoint':
185-
st += `MULTIPOINT(${TextEncoder.geoArrayPointToString(geo[i].coordinates)})`;
194+
st += `MULTIPOINT(${TextEncoder.geoArrayPointToString(item.coordinates)})`;
186195
break;
187196

188197
case 'MultiLineString':
189-
st += `MULTILINESTRING(${TextEncoder.geoMultiArrayPointToString(geo[i].coordinates)})`;
198+
st += `MULTILINESTRING(${TextEncoder.geoMultiArrayPointToString(item.coordinates)})`;
190199
break;
191200

192201
case 'MultiPolygon':
193-
st += `MULTIPOLYGON(${TextEncoder.geoMultiPolygonToString(geo[i].coordinates)})`;
202+
st += `MULTIPOLYGON(${TextEncoder.geoMultiPolygonToString(item.coordinates)})`;
194203
break;
195204
}
196205
}
@@ -199,59 +208,73 @@ class TextEncoder {
199208

200209
static geoMultiPolygonToString(coords) {
201210
if (!coords) return '';
202-
let st = '';
203-
for (let i = 0; i < coords.length; i++) {
204-
st += (i !== 0 ? ',(' : '(') + TextEncoder.geoMultiArrayPointToString(coords[i]) + ')';
211+
212+
const len = coords.length;
213+
if (len === 0) return '';
214+
215+
let st = '(';
216+
217+
for (let i = 0; i < len; i++) {
218+
if (i !== 0) st += ',(';
219+
st += TextEncoder.geoMultiArrayPointToString(coords[i]) + ')';
205220
}
221+
206222
return st;
207223
}
208224

209225
static geoMultiArrayPointToString(coords) {
210226
if (!coords) return '';
211-
let st = '';
212-
for (let i = 0; i < coords.length; i++) {
213-
st += (i !== 0 ? ',(' : '(') + TextEncoder.geoArrayPointToString(coords[i]) + ')';
227+
228+
const len = coords.length;
229+
if (len === 0) return '';
230+
231+
let st = '(';
232+
233+
for (let i = 0; i < len; i++) {
234+
if (i !== 0) st += ',(';
235+
st += TextEncoder.geoArrayPointToString(coords[i]) + ')';
214236
}
237+
215238
return st;
216239
}
217240

218241
static geoArrayPointToString(coords) {
219242
if (!coords) return '';
243+
244+
const len = coords.length;
245+
if (len === 0) return '';
246+
220247
let st = '';
221-
for (let i = 0; i < coords.length; i++) {
222-
st += (i !== 0 ? ',' : '') + TextEncoder.geoPointToString(coords[i]);
248+
249+
for (let i = 0; i < len; i++) {
250+
if (i !== 0) st += ',';
251+
st += TextEncoder.geoPointToString(coords[i]);
223252
}
253+
224254
return st;
225255
}
226256

227257
static geoPointToString(coords) {
228258
if (!coords) return '';
229-
return (isNaN(coords[0]) ? '' : coords[0]) + ' ' + (isNaN(coords[1]) ? '' : coords[1]);
259+
const x = isNaN(coords[0]) ? '' : coords[0];
260+
const y = isNaN(coords[1]) ? '' : coords[1];
261+
return x + ' ' + y;
230262
}
231263

232264
static getLocalDate(date) {
265+
const year = date.getFullYear();
266+
const month = date.getMonth() + 1;
267+
const day = date.getDate();
268+
const hours = date.getHours();
269+
const minutes = date.getMinutes();
270+
const seconds = date.getSeconds();
233271
const ms = date.getMilliseconds();
234272

235-
//return 'YYYY-MM-DD HH:MM:SS' datetime format
236-
//see https://mariadb.com/kb/en/library/datetime/
237-
let d =
238-
"'" +
239-
date.getFullYear() +
240-
'-' +
241-
(date.getMonth() + 1) +
242-
'-' +
243-
date.getDate() +
244-
' ' +
245-
date.getHours() +
246-
':' +
247-
date.getMinutes() +
248-
':' +
249-
date.getSeconds();
273+
const d = "'" + year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
274+
250275
if (ms === 0) return d + "'";
251276

252-
let res = `${ms}`;
253-
while (res.length < 3) res = '0' + res;
254-
return d + '.' + res + "'";
277+
return d + '.' + (ms < 10 ? '00' : ms < 100 ? '0' : '') + ms + "'";
255278
}
256279

257280
static getFixedFormatDate(date) {
@@ -263,9 +286,7 @@ class TextEncoder {
263286
const sec = date.getSeconds();
264287
const ms = date.getMilliseconds();
265288

266-
//return 'YYYY-MM-DD HH:MM:SS' datetime format
267-
//see https://mariadb.com/kb/en/library/datetime/
268-
return (
289+
let result =
269290
"'" +
270291
formatDigit(year, 4) +
271292
'-' +
@@ -277,10 +298,13 @@ class TextEncoder {
277298
':' +
278299
formatDigit(min, 2) +
279300
':' +
280-
formatDigit(sec, 2) +
281-
(ms > 0 ? '.' + formatDigit(ms, 3) : '') +
282-
"'"
283-
);
301+
formatDigit(sec, 2);
302+
303+
if (ms > 0) {
304+
result += '.' + formatDigit(ms, 3);
305+
}
306+
307+
return result + "'";
284308
}
285309
}
286310

0 commit comments

Comments
 (0)