Skip to content

Commit 7589e12

Browse files
committed
Use lazy hex cache
1 parent c08fa56 commit 7589e12

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/objectid.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class ObjectId extends BSONValue {
3232
/** @internal */
3333
private static index = Math.floor(Math.random() * 0xffffff);
3434

35-
static cacheHexString: boolean;
35+
static cacheHexString: boolean = true;
3636

3737
/** ObjectId Bytes @internal */
3838
private buffer!: Uint8Array;
@@ -111,6 +111,10 @@ export class ObjectId extends BSONValue {
111111
} else if (typeof workingId === 'string') {
112112
if (ObjectId.validateHexString(workingId)) {
113113
this.buffer = ByteUtils.fromHex(workingId);
114+
// If we are caching the hex string
115+
if (ObjectId.cacheHexString) {
116+
this.__id = workingId;
117+
}
114118
} else {
115119
throw new BSONError(
116120
'input must be a 24 character hex string, 12 byte Uint8Array, or an integer'
@@ -119,10 +123,6 @@ export class ObjectId extends BSONValue {
119123
} else {
120124
throw new BSONError('Argument passed in does not match the accepted types');
121125
}
122-
// If we are caching the hex string
123-
if (ObjectId.cacheHexString) {
124-
this.__id = ByteUtils.toHex(this.id);
125-
}
126126
}
127127

128128
/**

test/node/bson_test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1648,21 +1648,25 @@ describe('BSON', function () {
16481648
it('ObjectId should have a correct cached representation of the hexString', function (done) {
16491649
ObjectId.cacheHexString = true;
16501650
var a = new ObjectId();
1651+
a.toHexString();
16511652
var __id = a.__id;
16521653
expect(__id).to.equal(a.toHexString());
16531654

16541655
// hexString
16551656
a = new ObjectId(__id);
1657+
a.toHexString();
16561658
expect(__id).to.equal(a.toHexString());
16571659

16581660
// fromHexString
16591661
a = ObjectId.createFromHexString(__id);
1662+
a.toHexString();
16601663
expect(a.__id).to.equal(a.toHexString());
16611664
expect(__id).to.equal(a.toHexString());
16621665

16631666
// number
16641667
var genTime = a.generationTime;
16651668
a = new ObjectId(genTime);
1669+
a.toHexString();
16661670
__id = a.__id;
16671671
expect(__id).to.equal(a.toHexString());
16681672

@@ -1673,6 +1677,7 @@ describe('BSON', function () {
16731677

16741678
// createFromTime
16751679
a = ObjectId.createFromTime(genTime);
1680+
a.toHexString();
16761681
__id = a.__id;
16771682
expect(__id).to.equal(a.toHexString());
16781683
ObjectId.cacheHexString = false;
@@ -1861,7 +1866,7 @@ describe('BSON', function () {
18611866
);
18621867
expect(inspect(code)).to.equal(
18631868
/* eslint-disable */
1864-
`new Code(
1869+
`new Code(
18651870
'function iLoveJavaScript() {\\n' +
18661871
' do {\\n' +
18671872
" console.log('hello!');\\n" +

0 commit comments

Comments
 (0)