Skip to content

Commit 9a150e1

Browse files
authored
perf(NODE-5557): move DataView and Set allocation used for double parsing and utf8 validation to nested path (#611)
1 parent ec51256 commit 9a150e1

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/parser/deserializer.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ function deserializeObject(
164164
// Reflects utf-8 validation setting regardless of global or specific key validation
165165
let validationSetting: boolean;
166166
// Set of keys either to enable or disable validation on
167-
const utf8KeysSet = new Set();
167+
let utf8KeysSet;
168168

169169
// Check for boolean uniformity and empty validation option
170170
const utf8ValidatedKeys = validation.utf8;
@@ -190,6 +190,8 @@ function deserializeObject(
190190

191191
// Add keys to set that will either be validated or not based on validationSetting
192192
if (!globalUTFValidation) {
193+
utf8KeysSet = new Set();
194+
193195
for (const key of Object.keys(utf8ValidatedKeys)) {
194196
utf8KeysSet.add(key);
195197
}
@@ -216,8 +218,9 @@ function deserializeObject(
216218

217219
let isPossibleDBRef = isArray ? false : null;
218220

221+
let dataView;
222+
219223
// While we have more left data left keep parsing
220-
const dataview = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
221224
while (!done) {
222225
// Read the type
223226
const elementType = buffer[index++];
@@ -240,7 +243,7 @@ function deserializeObject(
240243

241244
// shouldValidateKey is true if the key should be validated, false otherwise
242245
let shouldValidateKey = true;
243-
if (globalUTFValidation || utf8KeysSet.has(name)) {
246+
if (globalUTFValidation || utf8KeysSet?.has(name)) {
244247
shouldValidateKey = validationSetting;
245248
} else {
246249
shouldValidateKey = !validationSetting;
@@ -284,10 +287,12 @@ function deserializeObject(
284287
(buffer[index++] << 16) |
285288
(buffer[index++] << 24);
286289
} else if (elementType === constants.BSON_DATA_NUMBER && promoteValues === false) {
287-
value = new Double(dataview.getFloat64(index, true));
290+
dataView ??= new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
291+
value = new Double(dataView.getFloat64(index, true));
288292
index = index + 8;
289293
} else if (elementType === constants.BSON_DATA_NUMBER) {
290-
value = dataview.getFloat64(index, true);
294+
dataView ??= new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
295+
value = dataView.getFloat64(index, true);
291296
index = index + 8;
292297
} else if (elementType === constants.BSON_DATA_DATE) {
293298
const lowBits =
@@ -300,6 +305,7 @@ function deserializeObject(
300305
(buffer[index++] << 8) |
301306
(buffer[index++] << 16) |
302307
(buffer[index++] << 24);
308+
303309
value = new Date(new Long(lowBits, highBits).toNumber());
304310
} else if (elementType === constants.BSON_DATA_BOOLEAN) {
305311
if (buffer[index] !== 0 && buffer[index] !== 1)

0 commit comments

Comments
 (0)