473,326 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Function expressions versus declarations

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
3 2205
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tim Conner | last post by:
How can I use regex to split an expression like the following : (Round(340/34.12)*2) into this list : ( Round ( 340
1
by: Reny | last post by:
How do i declare a function when i put option strict On
20
by: Joseph Wakeling | last post by:
Hello all, Here's a brief function from the aforementioned (and controversial) GNU Scientific Library (GSL), or rather, a slightly rewritten version of it that I've made. The function takes an...
56
by: Luke Matuszewski | last post by:
I am designing the library, which will hidden all its functions within singleton object ... So for clients they will use it like . eg. system.getElementWithId('ruler'); At library side, i...
5
by: goldfita | last post by:
I have the following two prototypes in separate source files. Both programs compile and work correctly. (1) void general_foo(void (*foo)(), int opt, ...); (2) static int...
14
by: António Marques | last post by:
Hi! I don't think I've ever been here, so I assume I'm addressing a bunch of nice people. Can you help me? I don't think there's a solution, but who knows. The thing is, picture a large...
2
by: Steven D'Aprano | last post by:
The timeit module is ideal for measuring small code snippets; I want to measure large function objects. Because the timeit module takes the code snippet argument as a string, it is quite handy...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.