---
schema_version: "1"
id: ded529f47696a8b186c9de10b2beb19072fa64b43744bd86fa6d619e85a2ac24
title: querySelector APIs Running With Lightning Locker
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/lwc/lightning-components-security/guides/locker/locker-queryselector.md
canonical_url: https://developer.salesforce.com/docs/platform/lightning-components-security/guide/locker-queryselector.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: a3cb78a7870411bea9e8ba0f64e16e5b025e890cc03382cf64fc8cd8eee8acb5
products:
  - Security for Lightning Components
  - lwc
---
# querySelector APIs Running With Lightning Locker

When Lightning Locker is enabled, components that use `element.querySelector` or `element.querySelectorAll` APIs to return elements can encounter memory leaks and performance problems with large DOM trees.

**note:** For Lightning web components, `element` refers to the `this.template` prefix. For Aura components, `element` refers to the `this` prefix.

When the component is removed from the DOM, the memory sometimes isn’t released. Garbage collection doesn’t clear the memory even if you manually invoke garbage collection in DevTools. The browser can use increasing amounts of memory, and potentially crash with an out-of-memory error.

The only workaround for this issue is to [enable Lightning Web Security](https://developer.salesforce.com/docs/platform/lightning-components-security/lws/lws-enable.md) in the org where the component runs. Garbage collection works correctly under LWS.

Alternatively, if you're developing Lightning web components, consider using [refs](/docs/platform/lwc/guide/create-components-dom-work.html#refs) instead of `querySelector`.

**See Also**

- [_Lightning Web Components Developer Guide_: Access Elements the Component Owns](/docs/platform/lwc/guide/create-components-dom-work.html)
