Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is better - Ext.get() or document.getElementById()

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() ?

like image 908
hop Avatar asked Dec 31 '11 16:12

hop


3 Answers

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.

like image 120
owlness Avatar answered Oct 25 '22 04:10

owlness


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 :)

like image 32
naveen Avatar answered Oct 25 '22 03:10

naveen


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.

like image 37
ziesemer Avatar answered Oct 25 '22 03:10

ziesemer