On Wednesday, February 16, 2011 10:39:36 AM UTC+1, Fran wrote:
>
> That was a good article of Ben Alman... Just wanted to mention that after
> doing some test I found out that using "new function" to immediately invoke
> the function is rather slow compare to the other two patterns, up to 50%
> slower y some cases.
>
Without a proper function body, '50%' has little meaning as this is just the
overhead of the vm creating a new context for the function invoked via `new`
while the `(fun...` expression are executed in the global scope.
As it has not been mentioned directly in this thread, the reason for having
the parens around the Function Expression (its actually the `(` the front
that is important) is that a Function Expression cannot begin a statement
(if it has an identifier it is a Function Declaration, if not it's a parse
error), and since only FE's can be invoked immediately some kind of
construct needs to be used to get it parsed as an FE instead of an FD. The
simplest way to do so is to put it in a Grouping Expression.
Where that Grouping Expression ends (after the FE, or after its invocation)
has no consequence for its parsing and invocation.
If you want to save characters, you can for instance do
+function(){}()
-function(){}()
~function(){}()
etc...
Sean
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]