What is better to use document.getElementById("myId")
or component.find("myId").getElement();
in terms of performance? What will be faster?
1 Answer
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.