![]() |
|
|
Performance Hintsvar StatementsUse var 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 rewritefunction sum(a) {
as function sum(a) {
This is not equivalent code because the second version does
not modify global variables result and i. 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 sum!).
ArraysUse the forms of the Array constructor that specify a size or take a list of initial elements. For example, the codevar a = new Array();
could be sped up by changing the constructor call to new Array(n). A constructor call like that indicates to
the runtime that a Java array should be used for the first n entries of the array. Similarly,
new
Array("a", "b", "c") or ["a", "b", "c"] will cause a 3-element
Java array to be allocated to hold the contents of the JavaScript array.
eval and new FunctionAvoid calling eval when possible. Calls to eval 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 codefunction MyObject(a) {
could be written more efficiently as function MyObject(a) {
Beginning with Rhino 1.4 Release 2, code
passed to eval and new Function will be interpreted rather than compiled to
class files.
withUsing the with 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.
back to top
|
|||||||
| Copyright © 1998 The Mozilla Organization. | ||||||||