By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,384 Members | 1,847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,384 IT Pros & Developers. It's quick & easy.

Function expressions versus declarations

P: n/a
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.

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. 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 so the performance issue seems moot - the test
involved calling each function several thousand times from another
function.

Is the test appropriate? Are there any other concerns besides code
style preference?
--
Rob
Aug 6 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.
What has to be considered is that function declarations are evaluated upon
variable instantiation, before execution, while function expressions are
evaluated during execution. The latter has the potential to slow down
execution. However, I have yet to see a Function object large enough to
make a considerable difference there.

Another point could be that function expressions are not supported by
earlier implementations, but that would appear to be moot today. In fact,
at this point the next revision of the ECMAScript Support Matrix would mark
this feature as safe to use, being supported since JavaScript 1.2 and
JScript 1.0, ECMAScript Ed. 1, and in its anonymous form since JavaScript
1.3, JScript 3.0, although not introduced before ECMAScript Ed. 3.
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. 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 so the performance issue seems moot - the test
involved calling each function several thousand times from another
function.

Is the test appropriate?
If that is what was meant. There is no way of knowing until you refer
directly to the exact place of the claim. And maybe not even then.

The first function declaration declares a function that, when called,
creates a Function object with a function expression after variable
instantiation, assigns the reference to it to a local variable, and returns
this reference.

On the other hand, the second function declaration declares a function that,
when called, creates a Function object upon variable instantiation and
assigns a reference to it to a property of the Variable Object of the local
execution context, and returns that reference.

So, as you can see, there is no significant difference in performance to be
expected here.
Are there any other concerns besides code style preference?
There are, see above.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Aug 6 '08 #2

P: n/a
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.

Aug 6 '08 #3

P: n/a
Thomas 'PointedEars' Lahn wrote:
RobG wrote:
[...]
>Is the test appropriate?

If that is what was meant. There is no way of knowing until you refer
directly to the exact place of the claim. And maybe not even then.
The thread is on a Google Group:

<URL:
http://groups.google.com.au/group/ip...1b9dcfb8?hl=en
>

--
Rob
Aug 6 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.