diff --git a/src/cmap/wire_protocol/on_demand/document.ts b/src/cmap/wire_protocol/on_demand/document.ts index 133c9e78ff9..c68e7059766 100644 --- a/src/cmap/wire_protocol/on_demand/document.ts +++ b/src/cmap/wire_protocol/on_demand/document.ts @@ -124,19 +124,12 @@ export class OnDemandDocument { } if (typeof name === 'number') { - if (this.isArray) { - if (name < this.elements.length) { - const element = this.elements[name]; - const cachedElement = { element, value: undefined }; - this.cache[name] = cachedElement; - this.indexFound[name] = true; - return cachedElement; - } else { - return null; - } - } else { - return null; + // Array elements are addressed directly by index, so there is nothing to speed up by + // caching them. + if (this.isArray && name < this.elements.length) { + return { element: this.elements[name], value: undefined }; } + return null; } for (let index = 0; index < this.elements.length; index++) { diff --git a/test/unit/cmap/wire_protocol/on_demand/document.test.ts b/test/unit/cmap/wire_protocol/on_demand/document.test.ts index 34e337f85a7..34b6e9a0114 100644 --- a/test/unit/cmap/wire_protocol/on_demand/document.test.ts +++ b/test/unit/cmap/wire_protocol/on_demand/document.test.ts @@ -99,6 +99,12 @@ describe('class OnDemandDocument', () => { expect(array.get(1, BSONType.int)).to.equal(1); }); + it('does not cache elements accessed by number', () => { + array.get(1, BSONType.int); + expect(array).to.not.have.nested.property('cache.1'); + expect(array).to.not.have.nested.property('indexFound.1'); + }); + it('does not support access by number for objects', () => { expect(document.get(233, BSONType.int)).to.be.null; expect(document.get('233', BSONType.int)).to.equal(3);