@@ -7,9 +7,6 @@ import { NumberUtils } from './utils/number_utils';
77// Unique sequence for the current process (initialized on first use)
88let PROCESS_UNIQUE : Uint8Array | null = null ;
99
10- /** ObjectId hexString cache @internal */
11- const __idCache = new WeakMap ( ) ; // TODO(NODE-6549): convert this to #__id private field when target updated to ES2022
12-
1310/** @public */
1411export interface ObjectIdLike {
1512 id : string | Uint8Array ;
@@ -35,11 +32,20 @@ export class ObjectId extends BSONValue {
3532 /** @internal */
3633 private static index = Math . floor ( Math . random ( ) * 0xffffff ) ;
3734
38- static cacheHexString : boolean ;
35+ static cacheHexString : boolean = false ;
3936
4037 /** ObjectId Bytes @internal */
4138 private buffer ! : Uint8Array ;
4239
40+ /**
41+ * If hex string caching is enabled, contains the cached hex string. Otherwise, is null.
42+ *
43+ * Note that #hexString is populated lazily, and as a result simply checking `this.#hexString != null` is
44+ * not sufficient to determine if caching is enabled. `ObjectId.prototype.isCached()` can be used to
45+ * determine if the hex string has been cached yet for an ObjectId.
46+ */
47+ #cachedHexString: string | null = null ;
48+
4349 /** To generate a new ObjectId, use ObjectId() with no argument. */
4450 constructor ( ) ;
4551 /**
@@ -107,7 +113,7 @@ export class ObjectId extends BSONValue {
107113 this . buffer = ByteUtils . fromHex ( workingId ) ;
108114 // If we are caching the hex string
109115 if ( ObjectId . cacheHexString ) {
110- __idCache . set ( this , workingId ) ;
116+ this . #cachedHexString = workingId ;
111117 }
112118 } else {
113119 throw new BSONError (
@@ -130,7 +136,7 @@ export class ObjectId extends BSONValue {
130136 set id ( value : Uint8Array ) {
131137 this . buffer = value ;
132138 if ( ObjectId . cacheHexString ) {
133- __idCache . set ( this , ByteUtils . toHex ( value ) ) ;
139+ this . #cachedHexString = ByteUtils . toHex ( value ) ;
134140 }
135141 }
136142
@@ -159,15 +165,12 @@ export class ObjectId extends BSONValue {
159165
160166 /** Returns the ObjectId id as a 24 lowercase character hex string representation */
161167 toHexString ( ) : string {
162- if ( ObjectId . cacheHexString ) {
163- const __id = __idCache . get ( this ) ;
164- if ( __id ) return __id ;
165- }
168+ if ( this . #cachedHexString) return this . #cachedHexString. toLowerCase ( ) ;
166169
167170 const hexString = ByteUtils . toHex ( this . id ) ;
168171
169172 if ( ObjectId . cacheHexString ) {
170- __idCache . set ( this , hexString ) ;
173+ this . #cachedHexString = hexString ;
171174 }
172175
173176 return hexString ;
@@ -365,9 +368,13 @@ export class ObjectId extends BSONValue {
365368 return new ObjectId ( doc . $oid ) ;
366369 }
367370
368- /** @internal */
371+ /**
372+ * @internal
373+ *
374+ * used for testing
375+ */
369376 private isCached ( ) : boolean {
370- return ObjectId . cacheHexString && __idCache . has ( this ) ;
377+ return ObjectId . cacheHexString && this . #cachedHexString != null ;
371378 }
372379
373380 /**
0 commit comments