What is better to use document.getElementById("myId") or component.find("myId").getElement(); in terms of performance? What will be faster?

up vote 7 down vote accepted

Regardless of performance, you should use component.find. Document query selectors aren't recommended in lightning, whereas component.find has explicit support:

Valid DOM Access

The following methods are valid DOM access because the elements are created by c:domLocker.

cmp.getElements()

Returns the elements in the DOM rendered by the component.

cmp.find()

Returns the div and button components, identified by their aura:id attributes.

cmp.find("div1").getElement()

Returns the DOM element for the div as c:domLocker created the div.

event.getSource().get("v.name")

Returns the name of the button that dispatched the event; in this case, myButton.

Notice the complete absence of query selectors in the enumeration of valid methods to access the DOM. It's not a supported approach, strictly speaking, even if it may work today.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.