On Aug 6, 2:00 pm, RobG wrote:
There has been a discussion on the iPhone web development
group where an opinion has been expressed that function
expressions are bad for performance and can be avoided by
using function declarations.
The fact that the 'demonstration' of this is fatally flawed has not gone
uncommented there. The OP there seems to have misperceived the
consequences of the poor or inappropriate use of function expressions as
a characteristic of expressions themselves.
There is a slightly odd assertion that function expressions involve the
runtime compiling of the function body whenever a function expressions
is evaluated. That is not the way function expressions are specified,
and almost certainly not how they are implemented.
For example (in a trivial case, the function body would
do processing depending on various parameters passed) instead of:
function foo(x) {
var a = function(){x};
return a;
}
one could use:
function foo(x) {
function a(){x};
return a;
}
The claim is that the declared internal function is more
efficient than the the assignment of an anonymous function.
There is nothing in ECMA 262 that suggests that it would be, and
previous testing by participants in this group have never managed to
demonstrate any significant differences in performance. (Unlike the use
of the Function constructor, which is easily the slowest method of
creating a function object, including sometimes being significantly
slower than eval-ing a function declaration).
Some implementations may favour one or the other with specific
optimizations, but function expressions are so common these days that
they are as likely to be the targets of such optimizations as function
declarations.
My testing shows that there isn't any significant difference
in performance between the two approaches in Firefox and that
in Safari the anonymous function version is faster
By much, and consitently?
When I test this I had to do a million iterations to get a worthwhile
timing and the differences were small then, and so were miniscule when
considered on a per-function instantiation basis.
so the performance issue seems moot -
That is not an unexpected outcome.
the test involved calling each function several thousand
times from another function.
Is the test appropriate?
That type/style of test probably is as valid as anything else that could
be attempted. I did not see any flaws in the specific test you posted to
the thread in question, except I would have pushed the loop length up as
far as it would go (which is usually up to he point that IE or Firefox
would put up one of those 'a script on this page .. " dialogs).
Are there any other concerns besides code
style preference?
Download size (without the 'var' and '=')?
Proposals for ES 3.1 and 4 will apparently not allow a declared function
to be re-assigned, but will allow a - var - value that may be a function
object to be re-assigned. So if you are going to be replacing the
function with another it might be a good idea to get into the habit of
creating it by assigning a function expression in the first place.
Remember that function declarations cannot appear inside blocks in
ECMAScript so conditional function creation requires the assignment of
function expressions.
Personally, when I can use a function declaration (when ECMAScript
syntax allows it, the need for the function is unconditional and there
is no intention to replace it) I do use one, but that is a style choice.
Richard.