10

What is the difference between Ext.get() and document.getElementById() in terms of performance? Will Ext.get() be slower as it may internally call document.getElementById() ? Or is there any specific advantage of using Ext.get() ?

hop
  • 2,518
  • 11
  • 40
  • 56

6 Answers6

18

The principle advantage of Ext.get over getElementById is that it returns to you an Ext.Element instance. This instance not only contains the DOM node reference that getElementById would give you but also significantly extends it - offering a suite of convenience methods, event normalization, and an ironing out of cross-browser differences.

On the surface getElementById may have some minuscule speed gain over Ext.get simply on the basis of one less function before getting to the same fundamental DOM call. However, in terms of overall performance what you do with the element after retrieval will likely have much more impact than the retrieval itself. Having the Ext.Element wrapper on hand may prove to be quite beneficial.

You may want to have a look at Ext.fly as well. This method is similar to Ext.get with exception that it returns to you a singleton Ext.Element instance. It won't be any good if you need to store the element for later use, but if you are doing simple, one-off operations against unique DOM nodes it may be cheaper than Ext.get.

owlness
  • 2,936
  • 18
  • 13
3

document.getElementById() is native JavaScript and so will be faster than Ext.get()

Now why Ext.get() is there at all,

document.getElementById() returns a DOM element, while Ext.get() returns an Ext object which is apt for chaining purposes.

And this is also the reason why jQuery have a $("#elm_id"). Please note that Ext.get() is also much easier to type :)

naveen
  • 53,448
  • 46
  • 161
  • 251
3

Ext.get() allows for using a String ID, an existing HTMLElement, or a Ext.Element - so it's a little bit more flexible. document.getElementById() only accepts the String ID.

That said, I'd just use document.getElementById() if it meets your needs. It's native to the browser and should be a little faster - and it's one less call that you're chaining yourself to a specific JavaScript framework with.

ziesemer
  • 27,712
  • 8
  • 86
  • 94
3

In terms of performance, native JS functions will always be faster.

However, I am not saying not to use JS Libraries, they are great as they:

  • reduce the time when writing your code
  • it make your code more readable
  • you write less code (reducing file-size and download time)

And in the end, maybe you even save time because less code means faster download and in some cases it could even beat the performance.

So yeah, it is the same to use one over the other, since in one hand you save time by performance ("document.getElementById()") and in the other you reduce file size and download time ("Ext.get()").

You can use both and there shouldn't be any noticeable difference.

ajax333221
  • 11,436
  • 16
  • 61
  • 95
1

As others have eluded to here the method used depends upon need, if you just want to get a reference to the dom element for some non Ext purpose you may as well use the native function, but if you intend to perform actions on the returned object in an Ext context, then Ext.get will return you an Element reference which offers additional methods.

Ext.get is shorthand for Ext.ComponentManager.get and whilst it is a call to a library function and may be less efficient is should be noted that there are ~180 methods available on Ext.Element, so if you need these it may be worth including wrapper call.

As owlness has mentioned, Ext.fly() is designed when you need to perform a single function on an element, eg. Ext.fly("myDiv").hide(); whereas Ext.get() is intended when you need a get a reference to an element for later use, eg. var something = Ext.get("myDiv"); then perhaps something.sort(); something.badger(); return something;

dougajmcdonald
  • 19,231
  • 12
  • 56
  • 89
0

I'm unfamiliar with the Ext library, but with vanilla Javascript, there's only a handful of ways to get a particular element; you can get it by its ID, search for it after getting all elements by a tag name (this is how JQuery gets elements by class name I believe), or, new to HTML5, get an element by a class name. There's a few other ways if you get creative ;) Just getting it by ID is the quickest, assuming you didn't save a local reference.

So, if all you're trying to do is get an element without doing whatever Ext.js does via that function call, vanilla Javascript will be much faster.

Jeffrey Sweeney
  • 5,986
  • 5
  • 24
  • 32