Skip to content

Commit 61537f5

Browse files
SeanReeceW-A-James
andauthored
perf(NODE-6356): Improve serialization performance (#709)
Co-authored-by: Warren James <[email protected]>
1 parent e584fbb commit 61537f5

File tree

7 files changed

+249
-202
lines changed

7 files changed

+249
-202
lines changed

src/bson_value.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { BSON_MAJOR_VERSION } from './constants';
22
import { type InspectFn } from './parser/utils';
3+
import { BSON_VERSION_SYMBOL } from './constants';
34

45
/** @public */
56
export abstract class BSONValue {
67
/** @public */
78
public abstract get _bsontype(): string;
89

910
/** @internal */
10-
get [Symbol.for('@@mdb.bson.version')](): typeof BSON_MAJOR_VERSION {
11+
get [BSON_VERSION_SYMBOL](): typeof BSON_MAJOR_VERSION {
1112
return BSON_MAJOR_VERSION;
1213
}
1314

src/constants.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/** @internal */
22
export const BSON_MAJOR_VERSION = 6;
33

4+
/** @internal */
5+
export const BSON_VERSION_SYMBOL = Symbol.for('@@mdb.bson.version');
6+
47
/** @internal */
58
export const BSON_INT32_MAX = 0x7fffffff;
69
/** @internal */

src/decimal128.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export class Decimal128 extends BSONValue {
142142
super();
143143
if (typeof bytes === 'string') {
144144
this.bytes = Decimal128.fromString(bytes).bytes;
145-
} else if (isUint8Array(bytes)) {
145+
} else if (bytes instanceof Uint8Array || isUint8Array(bytes)) {
146146
if (bytes.byteLength !== 16) {
147147
throw new BSONError('Decimal128 must take a Buffer of 16 bytes');
148148
}

src/extended_json.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
BSON_INT32_MIN,
77
BSON_INT64_MAX,
88
BSON_INT64_MIN,
9-
BSON_MAJOR_VERSION
9+
BSON_MAJOR_VERSION,
10+
BSON_VERSION_SYMBOL
1011
} from './constants';
1112
import { DBRef, isDBRefLike } from './db_ref';
1213
import { Decimal128 } from './decimal128';
@@ -358,7 +359,7 @@ function serializeDocument(doc: any, options: EJSONSerializeOptions) {
358359
doc != null &&
359360
typeof doc === 'object' &&
360361
typeof doc._bsontype === 'string' &&
361-
doc[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION
362+
doc[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION
362363
) {
363364
throw new BSONVersionError();
364365
} else if (isBSONType(doc)) {

src/parser/calculate_size.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function calculateElement(
8181
if (
8282
value != null &&
8383
typeof value._bsontype === 'string' &&
84-
value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
84+
value[constants.BSON_VERSION_SYMBOL] !== constants.BSON_MAJOR_VERSION
8585
) {
8686
throw new BSONVersionError();
8787
} else if (value == null || value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {

0 commit comments

Comments
 (0)