473,379 Members | 1,201 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,379 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 2207
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
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.