1
votes

I am new to Extjs and using Extjs v4.2.0.

To find an object I can use either Ext.getCmp(id) or Ext.ComponentQuery.query(attribute).

Which one is better to use and faster?

2
focus on the 'better' and worry about 'faster' only if what you are doing turns out to be slowJonnyRaa

2 Answers

6
votes

The best is to avoid using any of this two directly. Ext.getCmp is especially considered bad "code smell". You should strive to organize your code in such a way that you don't need them.

Component queries are elegant, but you should use them either from a parent container (thus reducing the research tree and allowing you to leverage relative itemId), or from a controller. It is my guess that controllers are the true reason why component queries have been added to Ext4.

3
votes

getCmp will be faster, since it's a simple hash lookup. When you use query, it has to parse and then execute the query, so for a simple id only, getCmp is better.

However, be wary of using id's, since they need to be globally unique. It's only a good idea to use them in cases when you know there will only be one, for example a login window, or your main app container.