前段時(shí)間被IE和JavaScript腳本引擎的Memory Leak問(wèn)題弄得郁悶壞了,不過(guò)幸好現(xiàn)在總算是柳暗花明了,并且找到了一些IE中使用腳本避免ML問(wèn)題的方法。繼續(xù)研究JavaScript的編寫,有發(fā)現(xiàn)一些不算ML問(wèn)題,但是可以節(jié)約IE內(nèi)存使用的方法,在此和大家討論討論。
我們?cè)?a href=/itjie/Javajishu/ target=_blank class=infotextkey>JavaScript中編寫代碼,對(duì)于定義函數(shù)的語(yǔ)句:
function foo()
{
// TODO: . . .
return x;
}可以說(shuō)是在熟悉不過(guò)了。當(dāng)然除了這種定義函數(shù)的方法,我們還有另外幾種方法也能定義函數(shù):
var foo = function()
{
// TODO: . . .
return x;
}
var foo = new Function('{/*todo*/return x;}'); 后兩種方法定義的JavaScript函數(shù),在調(diào)用起來(lái)和第一種沒(méi)有任何效果上的區(qū)別。
不過(guò)由于JavaScript是解釋性語(yǔ)言,當(dāng)我們定義一個(gè)函數(shù)的時(shí)候,解析引擎生成一個(gè)Function對(duì)象實(shí)例,然后把函數(shù)內(nèi)容保存下來(lái)。所以每執(zhí)行一次函數(shù)定義語(yǔ)句,就會(huì)生成一個(gè)函數(shù)。而不像編譯語(yǔ)言,一個(gè)函數(shù)編譯一次后就被任何語(yǔ)句調(diào)用。啊?難道JavaScript不能調(diào)用定義好的函數(shù)?不是這個(gè)意思了,當(dāng)我們?cè)谥谱?a href=/itjie/Javajishu/ target=_blank class=infotextkey>JavaScript控件時(shí),如果動(dòng)態(tài)輸出DHTML來(lái)作為控件的內(nèi)容,就容易出現(xiàn)這樣的問(wèn)題。比如我們?cè)谝粋€(gè)HTML對(duì)象生成過(guò)程中,使用了inline方式定義的函數(shù),那么這個(gè)元素生成幾次,那個(gè)函數(shù)也就要同時(shí)生成幾次。
function TestObject.prototype.Render(doc, id)
{
var span = doc.createElement('SPAN');
span.Object = this;
this.m_Element = span;

if ( id == "NamedMethod" )
{
span.onclick = asdf;
}
else
{
span.onclick = function()
{
var asdf01 = ['a', 's', 'd', 'f'];
var asdf02 = ['a', 's', 'd', 'f'];
var asdf03 = ['a', 's', 'd', 'f'];
var asdf04 = ['a', 's', 'd', 'f'];
var asdf05 = ['a', 's', 'd', 'f'];
var asdf06 = ['a', 's', 'd', 'f'];
var asdf07 = ['a', 's', 'd', 'f'];
var asdf08 = ['a', 's', 'd', 'f'];
var asdf09 = ['a', 's', 'd', 'f'];
var asdf10 = ['a', 's', 'd', 'f'];
var asdf11 = ['a', 's', 'd', 'f'];
var asdf12 = ['a', 's', 'd', 'f'];
};
}
span.Name = this.m_Description;
span.innerText = this.m_Name;
span.style.display = 'block';
return span;
} 函數(shù)span.onclick = function()中的內(nèi)容是用來(lái)占位置的,這樣inline方式定義函數(shù),每次Render()都就會(huì)生成一個(gè)新的函數(shù)對(duì)象。使用inline方式有什么不好呢?當(dāng)對(duì)象實(shí)例多了的時(shí)候,會(huì)很明顯的浪費(fèi)內(nèi)存空間呀,試驗(yàn)數(shù)據(jù)如下:
| Normal Method | Inline Method |
---|
|
Initialized | 27.4 M | 27.4 M |
Rendered | 33.4 M | 35.2 M |
// IE消耗的內(nèi)存數(shù)量(PM+VM)
單看絕對(duì)內(nèi)存消耗差別不大,可是如果看相對(duì)內(nèi)存消耗:(35.2-33.4)/(33.4-27.4) = 30% !!!,還是很可觀的了,而且如果方法本省越大,inline時(shí)冗余數(shù)據(jù)就越多。
附測(cè)試代碼:
<html>
<head>
<title>JScript Function Spending</title>
<meta name="author" content="birdshome@博客園" />
</head>
<body onunload="ReleaseElements()">
<button id="NamedMethod" onclick="GenerateObjects(this)">
Append Normal Elements</button>
<button id="AnonymousMethod" onclick="GenerateObjects(this)">
Append Inline Elements</button>
<div id="container">
</div>

<script language="Javascript">
function GenerateObjects(elmt)


{
var room = document.getElementById('container');
for ( var i=0 ; i < 1000 ; ++i )

{
var obj = new TestObject('__Object__' + i);
room.appendChild(obj.Render(document, elmt.id));
}
}

function TestObject(name)


{
this.m_Name = name;
this.m_Description = '';
this.m_Element = null;
this.toString = function()

{
return '[class TestObject]';
}
}

function TestObject.prototype.Render(doc, id)


{
var span = doc.createElement('SPAN');
span.Object = this;
this.m_Element = span;

if ( id == "NamedMethod" )

{
span.onclick = asdf;
}
else

{
span.onclick = function()

{
var asdf01 = ['a', 's', 'd', 'f'];
var asdf02 = ['a', 's', 'd', 'f'];
var asdf03 = ['a', 's', 'd', 'f'];
var asdf04 = ['a', 's', 'd', 'f'];
var asdf05 = ['a', 's', 'd', 'f'];
var asdf06 = ['a', 's', 'd', 'f'];
var asdf07 = ['a', 's', 'd', 'f'];
var asdf08 = ['a', 's', 'd', 'f'];
var asdf09 = ['a', 's', 'd', 'f'];
var asdf10 = ['a', 's', 'd', 'f'];
var asdf11 = ['a', 's', 'd', 'f'];
var asdf12 = ['a', 's', 'd', 'f'];
};
}
span.Name = this.m_Description;
span.innerText = this.m_Name;
span.style.display = 'block';
return span;
}

function asdf()


{
var asdf01 = ['a', 's', 'd', 'f'];
var asdf02 = ['a', 's', 'd', 'f'];
var asdf03 = ['a', 's', 'd', 'f'];
var asdf04 = ['a', 's', 'd', 'f'];
var asdf05 = ['a', 's', 'd', 'f'];
var asdf06 = ['a', 's', 'd', 'f'];
var asdf07 = ['a', 's', 'd', 'f'];
var asdf08 = ['a', 's', 'd', 'f'];
var asdf09 = ['a', 's', 'd', 'f'];
var asdf10 = ['a', 's', 'd', 'f'];
var asdf11 = ['a', 's', 'd', 'f'];
var asdf12 = ['a', 's', 'd', 'f'];
}
</script>

<script language="Javascript">
function ReleaseElements()


{
var room = document.getElementById('container');
var spans = room.all.tags('SPAN');
for ( var i=0 ; i < spans.length ; ++i )

{
spans[i].Object = '';
}
}
</script>
</body>
</html>

JavaScript技術(shù):在JavaScript中使用inline函數(shù)的問(wèn)題,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。