@@ -7,6 +7,9 @@ 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+
1013/** @public */
1114export interface ObjectIdLike {
1215 id : string | Uint8Array ;
@@ -32,20 +35,11 @@ export class ObjectId extends BSONValue {
3235 /** @internal */
3336 private static index = Math . floor ( Math . random ( ) * 0xffffff ) ;
3437
35- static cacheHexString : boolean = false ;
38+ static cacheHexString : boolean ;
3639
3740 /** ObjectId Bytes @internal */
3841 private buffer ! : Uint8Array ;
3942
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-
4943 /** To generate a new ObjectId, use ObjectId() with no argument. */
5044 constructor ( ) ;
5145 /**
@@ -113,7 +107,7 @@ export class ObjectId extends BSONValue {
113107 this . buffer = ByteUtils . fromHex ( workingId ) ;
114108 // If we are caching the hex string
115109 if ( ObjectId . cacheHexString ) {
116- this . #cachedHexString = workingId ;
110+ __idCache . set ( this , workingId ) ;
117111 }
118112 } else {
119113 throw new BSONError (
@@ -136,7 +130,7 @@ export class ObjectId extends BSONValue {
136130 set id ( value : Uint8Array ) {
137131 this . buffer = value ;
138132 if ( ObjectId . cacheHexString ) {
139- this . #cachedHexString = ByteUtils . toHex ( value ) ;
133+ __idCache . set ( this , ByteUtils . toHex ( value ) ) ;
140134 }
141135 }
142136
@@ -165,12 +159,15 @@ export class ObjectId extends BSONValue {
165159
166160 /** Returns the ObjectId id as a 24 lowercase character hex string representation */
167161 toHexString ( ) : string {
168- if ( this . #cachedHexString) return this . #cachedHexString. toLowerCase ( ) ;
162+ if ( ObjectId . cacheHexString ) {
163+ const __id = __idCache . get ( this ) ;
164+ if ( __id ) return __id ;
165+ }
169166
170167 const hexString = ByteUtils . toHex ( this . id ) ;
171168
172169 if ( ObjectId . cacheHexString ) {
173- this . #cachedHexString = hexString ;
170+ __idCache . set ( this , hexString ) ;
174171 }
175172
176173 return hexString ;
@@ -368,13 +365,9 @@ export class ObjectId extends BSONValue {
368365 return new ObjectId ( doc . $oid ) ;
369366 }
370367
371- /**
372- * @internal
373- *
374- * used for testing
375- */
368+ /** @internal */
376369 private isCached ( ) : boolean {
377- return ObjectId . cacheHexString && this . #cachedHexString != null ;
370+ return ObjectId . cacheHexString && __idCache . has ( this ) ;
378371 }
379372
380373 /**
0 commit comments