`

Ext.get()与Ext.fly()的区别

 
阅读更多

 从一开始接触Ext就看到有Ext.fly这个函数,当时觉得这个跟Ext.get没什么区别,加之当时对JS性能相关问题认识肤浅,也一直没有在意其区别,在learning extjs中,看到了有专门对Ext.fly特别强调的一处:

This isn't exactly a speed tip, but is more about conserving memory by using 
something called a "flyweight" to perform simple tasks, which results in higher speed 
by not clogging up the browser's memory


大概意思也就是Ext.Fly采用flyweight模式使所有fly出来的元素共享内存,可以提高程序执行速度,减少内存占用。

这 段话激起了我对这个函数的兴趣,毕竟近段时间一直在搞JS性能优化相关问题,对“内存”这个字眼非常敏感。大概看了下Ext源码对get和fly实现的部 分,然后在网上查看了一些资料,终于对他们之间的异同有了个比较深入的认识。 Ext的官方开发人员给出了如下的解释:

Ext.Element wraps a lot of functionality around DOM element/node, for example functions like hide, show, all animation stuff, dimensions getting and setting function and a lot more.

Ext.Element keeps reference to DOM element it is wrapped around in dom property. Once you have an Ext.Element (e.g. you call Ext.get('some-id') it is an instance of Element class and you can work with it as such.

Now, imagine that you need to hide 1000 DOM nodes, you call 1000 times Ext.get('some-one-of-1000-id').hide() so you create 1000 instances of Element just to call one function: hide.

Ext.fly is one instance of Ext.Element with "replaceable" DOM node it is wrapped around. If you call 1000 times Ext.fly('some-one-of-1000-id').hide() you 1000 times replace dom property of one instance of Ext.Element.

Result: higher performance, lower memory usage.

You only need to keep in mind that you cannot keep Element returned by Ext.fly for later use as it's dom will sooner or later gets replaced by another one.

这段话中,大致的意思如下:

Ext.Element 是Ext对Dom元素的一个强有力封装,它封装了很多方便对dom操作的接口(并通过Element的dom属性引用对应的 dom元素),因此每创建一个Element元素都将消耗不少的内存(主要是大量的操作接口消耗),因此如果创建过多的Element元素必然导致内存占 用的剧增和系统性能的下降。

Ext.get和Ext.fly返回的都是一个Element对象,但是Ext.get返回的是一个独立的Element,拥有自己独立的操作接口 封装,可以将其返回值保存到变量中,以便以后调用操作等,这样为重用带来了方便。但是它的一个很大缺点就是内存消耗问题,假如调用 Ext.get(id)1000次,则会在内存中创建1000个独立Element,其内存占用可想而知。 但是很多时候我们可能仅仅只是对该dom元素执 行一次很简单的操作,如隐藏(hide),这样如果每次都创建一个独立Element放在内存中,实在是对内存的巨大浪费,因此当我们在只需要执行一次操 作或者一个很简单的操作时,采用Ext.get就显得很不合理。Ext.fly正是为了解决这个问题而出现,它通过使每次创建的Element共享内存中 的一套操作接口来达到节省内存的效果。

下面来看Ext.fly的实现代码(我简单加了一些注释):

var flyFn = function(){};

flyFn.prototype = El.prototype;

var _cls = new flyFn(); //将Element的所有操作接口放在_cls中

// dom is optional

El.Flyweight = function(dom){ this.dom = dom; }; //仅包含一个dom属性的Object

El.Flyweight.prototype = _cls; //将操作接口复制给Element实例对象

El.Flyweight.prototype.isFlyweight = true; //标志该Element是flyweight对象

El._flyweights = {}; //flyweight对象缓存容器

El.fly = function(el, named){ named = named || '_global'; el = Ext.getDom(el); //取得dom对象

if(!el){ return null; }

if(!El._flyweights[named]){ El._flyweights[named] = new El.Flyweight();

//仅在第一次调用Ext.fly时创建一个Flyweight对象并缓存

}

El._flyweights[named].dom = el; //将flyweight对象的dom属性指向该el

return El._flyweights[named];

};

从上面的代码不难看出,仅在第一次调用Ext.fly时创建一个Flyweight 对象(该对象包含了Element的所有操作接口)并将其缓存, 之后的所有fly操作都只是修改该flyweight对象的dom属性,每次fly返回的结果都是共享的同一个flyweight对象。这样每次fly返 回的Element相比Ext.get而言,减少了每次创建Element时对大量的操作接口的创建。所有fly的对象都共享一套Element操作接 口,内存占用自然少了很多,而且执行速度也得到了提升。在大量的创建操作中效果会更加明显。

由于fly的操作原理,我们不能将fly的返回结果保存在变量中以便重用,因为每次fly操作都将可能改变该变量的dom指向。如下面的代码就是不正确的:

var my_id = Ext.fly('my_id');

Ext.fly('another_id'); //此时my_id的dom引用已经变为another_id

my_id.highlight('FF0000',{ //此处的操作将是对another_id元素的操作

endColor:'0000FF', duration: 3

});

在以后使用中,一定要合理的利用Ext.get和Ext.fly,避免滥用Ext.get这个“重量级”的方法。

分享到:
评论

相关推荐

    Ext.get与Ext.fly的区别

    Ext.get与Ext.fly的区别与用法

    Ext.get与Ext.fly 的区别

    NULL 博文链接:https://canfly2010.iteye.com/blog/407474

    ext初级入门

    1.Ext.get var el = Ext.get('myElementId');//获取元素,等同于document.getElementById('myElementId');//会缓存 2. Ext.fly var el = Ext.fly('myElementId')//不需要缓存。 注:享元模式(Flyweight...

    Ext Js权威指南(.zip.001

    6.5.2 ext.dom.compositeelement与ext.dom.compositeelementlite的区别 / 279 6.5.3 操作元素集合 / 279 6.6 综合实例:可折叠的面板accordion / 280 6.7 本章小结 / 283 第7章 数据交互 / 284 7.1 数据交互...

    EXTJS总结.txt

    1.Ext.get var el = Ext.get('myElementId');//获取元素,等同于document.getElementById('myElementId');//会缓存 2. Ext.fly var el = Ext.fly('myElementId')//不需要缓存。 注:享元模式(Flyweight Design ...

    ExtJs使用总结(非常详细)

    一、获取元素(Getting Elements) 1.Ext.get var el = Ext.get(‘myElementId’);//获取元素,等同于document.getElementById(‘myElementId’);//会缓存 2. Ext.fly var el = Ext.fly(‘myElementId’)//不需要...

    extjs 学习笔记(二) Ext.Element类

    上个系列中,我们用到了Ext.fly来获得一个Element对象,其实Ext.fly是Ext.Element.fly的简写形式。类似的方法还有一个Ext.get,是Ext.Element.get的简写。这两个方法都可以用使用Dom元素或者Dom元素的id作为参数来...

    Midnight Commander Win32 port

    This is version 4 of the Midnight Commander, a free Norton Commander Clone with many useful features. The Midnight Commander comes with ... on the fly, coloring mail messages and more.

    ImpREC 1.7c

    - Fixed bug in "Fix dump" renaming (with the char '_' before the ext) - Added Original First Thunks rebuilder (in Options) - Fixed bug on disabled editboxes (you could edit them.... erm) - Added ...

Global site tag (gtag.js) - Google Analytics