nboyd%atg.com 348077ce22 Propagate formatting changes from the mozilla-org CVS repository
git-svn-id: svn://10.0.0.236/trunk@215218 18797224-902f-48f8-a5cc-f745e15eee43
2006-11-13 21:03:34 +00:00

155 lines
5.2 KiB
HTML

<!-- ***** BEGIN LICENSE BLOCK *****
- Version: MPL 1.1/GPL 2.0
-
- The contents of this file are subject to the Mozilla Public License Version
- 1.1 (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
- http://www.mozilla.org/MPL/
-
- Software distributed under the License is distributed on an "AS IS" basis,
- WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- for the specific language governing rights and limitations under the
- License.
-
- The Original Code is Rhino code, released May 6, 1999.
-
- The Initial Developer of the Original Code is
- Netscape Communications Corporation.
- Portions created by the Initial Developer are Copyright (C) 1997-1999
- the Initial Developer. All Rights Reserved.
-
- Contributor(s):
-
- Alternatively, the contents of this file may be used under the terms of
- the GNU General Public License Version 2 or later (the "GPL"), in which
- case the provisions of the GPL are applicable instead of those above. If
- you wish to allow use of your version of this file only under the terms of
- the GPL and not to allow others to use your version of this file under the
- MPL, indicate your decision by deleting the provisions above and replacing
- them with the notice and other provisions required by the GPL. If you do
- not delete the provisions above, a recipient may use your version of this
- file under either the MPL or the GPL.
-
- ***** END LICENSE BLOCK ***** -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Language" content="en">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta name="Author" content="Norris Boyd">
<title>Performance Hints</title>
<link rel="up" href="./" title="Rhino project page">
<link rel="section" href="#varstatements" title="Var statements">
<link rel="section" href="#arrays" title="Arrays">
<link rel="section" href="#eval" title="eval">
<link rel="section" href="#with" title="with">
</head>
<body>
<p class="crumbs"><em>You are here:</em> <a href="./">Rhino project page</a> &gt; <strong>Performance Hints</strong></p>
<h1 style="text-align: center;">Performance Hints</h1>
<h3><a name="varstatements" id="varstatements"><code>var</code> Statements</a></h3>
<p>Use <code>var</code> statements when possible. Not only is it good
programming practice, it can speed up your code by allowing the compiler
to generate special code to access the variables. For example, you could
rewrite</p>
<pre class="code">
function sum(a) {
result = 0;
for (i=0; i &lt; a.length; i++)
result += a[i];
return result;
}
</pre>
<p>as</p>
<pre class="code">
function sum(a) {
var result = 0;
for (var i=0; i &lt; a.length; i++)
result += a[i];
return result;
}
</pre>
<p>This is not equivalent code because the second version does
not modify global variables <code>result</code> and <code>i</code>.
However, if you don't intend for any other function to access these
variables, then storing them globally is probably wrong anyway
(what if you called another function that had a loop like the one
in <code>sum</code>!).</p>
<h3><a name="arrays" id="arrays">Arrays</a></h3>
<p>Use the forms of the Array constructor that specify a size or
take a list of initial elements. For example, the code</p>
<pre class="code">
var a = new Array();
for (var i=0; i &lt; n; i++)
a[i] = i;
</pre>
<p>could be sped up by changing the constructor call to
<code>new Array(n)</code>. A constructor call like that indicates
to the runtime that a Java array should be used for the first
<var>n</var> entries of the array. Similarly,<br><br>
<code>new Array(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;)</code><br>
or<br>
<code>[&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]</code><br><br>
will cause a 3-element Java array to be allocated to hold the
contents of the JavaScript array.</p>
<h3><a name="eval" id="eval"><code>eval</code> and <code>new Function</code></a></h3>
<p>Avoid calling <code>eval</code> when possible. Calls to
<code>eval</code> are slow because the script being executed must
be compiled. Constructing a new function object can be slow for
the same reason, while function expressions are more efficient
because the function can be compiled. For example, the code</p>
<pre class="code">
function MyObject(a) {
this.s = a;
this.toString = new Function(&quot;return this.s&quot;);
}
</pre>
<p>could be written more efficiently as</p>
<pre class="code">
function MyObject(a) {
this.s = a;
this.toString = function () { return this.s }
}
</pre>
<p>Beginning with Rhino 1.4 Release 2, code passed to eval and
new Function will be interpreted rather than compiled to class
files.</p>
<h3><a name="with" id="with"><code>with</code></a></h3>
<p>Using the <code>with</code> statement prevents the compiler
from generating code for fast access to local variables. You're
probably better off explicitly accessing any properties of the
object.</p>
</body>
</html>