前段时间被IE和JavaScript脚本引擎的Memory Leak问题弄得郁闷坏了,不过幸好现在总算是柳暗花明了,并且找到了一些IE中使用脚本避免ML问题的方法。继续研究JavaScript的编写,有发现一些不算ML问题,但是可以节约IE内存使用的方法,在此和大家讨论讨论。
我们在JavaScript中编写代码,对于定义函数的语句:
function foo()
{
// TODO: . . .
return x;
}
可以说是在熟悉不过了。当然除了这种定义函数的方法,我们还有另外几种方法也能定义函数:
var foo = function()
{
// TODO: . . .
return x;
}
var foo = new Function('{/*todo*/return x;}');
后两种方法定义的JavaScript函数,在调用起来和第一种没有任何效果上的区别。
不过由于JavaScript是解释性语言,当我们定义一个函数的时候,解析引擎生成一个Function对象实例,然后把函数内容保存下来。所以每执行一次函数定义语句,就会生成一个函数。而不像编译语言,一个函数编译一次后就被任何语句调用。啊?难道JavaScript不能调用定义好的函数?不是这个意思了,当我们在制作JavaScript控件时,如果动态输出DHTML来作为控件的内容,就容易出现这样的问题。比如我们在一个HTML对象生成过程中,使用了inline方式定义的函数,那么这个元素生成几次,那个函数也就要同时生成几次。
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;
}
函数span.onclick = function()中的内容是用来占位置的,这样inline方式定义函数,每次Render()都就会生成一个新的函数对象。使用inline方式有什么不好呢?当对象实例多了的时候,会很明显的浪费内存空间呀,试验数据如下:
|
Normal Method |
Inline Method |
|
Initialized |
27.4 M |
27.4 M |
Rendered |
33.4 M |
35.2 M |
// Amount of memory consumed by IE (PM VM)
There is not much difference in absolute memory consumption alone, but if you look at relative memory consumption: (35.2 -33.4)/(33.4-27.4) =
30% !!!, which is still very impressive, and the larger the method itself, the more redundant data will be inline.
附测试代码:
<html>
<head>
<title>JScript Function Spendingtitle>
<meta name="author" content="birdshome@博客园" />
head>
<body onunload="ReleaseElements()">
<button id="NamedMethod" onclick="GenerateObjects(this)">
Append Normal Elementsbutton>
<button id="AnonymousMethod" onclick="GenerateObjects(this)">
Append Inline Elementsbutton>
<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>