469,623 Members | 1,971 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

function pointers vs direct calling in javascript

Hi,
I wanted to know the performance of calling a function pointer v/s a
normal function call in javascript in a scenario where there are
multiple calls in the js to the same function. Please share your
thoughts if someone has worked on this.

Thanks,
Sampat.
Nov 20 '07 #1
15 3978
Sampat said the following on 11/20/2007 6:24 PM:
Hi,
I wanted to know the performance of calling a function pointer v/s a
normal function call in javascript in a scenario where there are
multiple calls in the js to the same function. Please share your
thoughts if someone has worked on this.
You don't need testing to figure it out, although testing will prove it.
The function pointer is *always* going to be slower. It has to be slower
since it it doing more than a plain function call. You are telling the
browser "Do this, then do that" versus "do that" and the first will
*always* be slower.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 21 '07 #2
On Nov 21, 9:24 am, Sampat <sampatdi...@gmail.comwrote:
Hi,
I wanted to know the performance of calling a function pointer v/s a
normal function call in javascript in a scenario where there are
multiple calls in the js to the same function. Please share your
thoughts if someone has worked on this.
I don't know what you mean by "function pointer", there is no such
term in javascript. If you mean is it better to assign a reference to
a function rather than use a long lookup chain every time, then the
answer is yes.

e.g. If you need to call the following method a number of times:

baseObj.childObj.method();
then you can assign a reference to a local variable:

var x = baseObj.childObj.method;
x();

That should be faster, but probably only if you have to call the
method more than twice within the same execution context. You will
likely only be able to measure the difference if you call the function
10,000 times or so, and users likely won't notice a difference until
you call it 100,000 times (or so).

However, if you mean by "function pointer" that you wrap a method in a
calling function like:

var x = function(){
baseObj.childObj.method();
}
then see Randy's answer.
--
Rob
Nov 21 '07 #3
On Nov 21, 1:35 am, Randy Webb <HikksNotAtH...@aol.comwrote:
Sampat said the following on 11/20/2007 6:24 PM:
Hi,
I wanted to know the performance of calling a function pointer v/s a
normal function call in javascript in a scenario where there are
multiple calls in the js to the same function. Please share your
thoughts if someone has worked on this.

You don't need testing to figure it out, although testing will prove it.
The function pointer is *always* going to be slower. It has to be slower
since it it doing more than a plain function call. You are telling the
browser "Do this, then do that" versus "do that" and the first will
*always* be slower.
I don't think I understand that. What's "this", and what is "that"?

Darko
Nov 21 '07 #4
On Nov 21, 9:24 am, Sampat <sampatdi...@gmail.comwrote:
>
Hi,
I wanted to know the performance of calling a function pointer v/s a
normal function call in javascript in a scenario where there are
multiple calls in the js to the same function. Please share your
thoughts if someone has worked on this.
My guess is that you think the second version below is using a
"function pointer". Both examples evaluate in about the same time in
my Firefox and I'm glad they do because I would be very confused if
they didn't!

----------------------------------------------------------------

function foo() {};

var tic = (new Date()).getTime();

for (var i=0; i<1e6; i++) {
foo();
}

alert((new Date()).getTime() - tic);

----------------------------------------------------------------

var foo = function() {};

var tic = (new Date()).getTime();

for (var i=0; i<1e6; i++) {
foo();
}

alert((new Date()).getTime() - tic);

----------------------------------------------------------------

In JavaScript functions are always first class data types so in the
above two examples the identifier "foo" points/refers to the function
object that is defined. The differences in the two examples are the
syntax and also when the function objects are ready to be called when
the JavaScript is first parsed and evaluated.

Peter
Nov 21 '07 #5
RobG said the following on 11/20/2007 9:33 PM:
On Nov 21, 9:24 am, Sampat <sampatdi...@gmail.comwrote:
>Hi,
I wanted to know the performance of calling a function pointer v/s a
normal function call in javascript in a scenario where there are
multiple calls in the js to the same function. Please share your
thoughts if someone has worked on this.

I don't know what you mean by "function pointer",
If you look at the " llegal operation on WrappedNative prototype object"
thread, you can get a better idea of what he is referring to. He is
referring to a wrapper function.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 21 '07 #6
On Nov 21, 8:26 am, Randy Webb <HikksNotAtH...@aol.comwrote:
var start2=new Date();
for (var i2=0;i2<100000;i2++){
var tempVar2 = gEBI('myDiv')
var tempVar2 = null;}
Hello Randy.

A quick question, why the 'var' keyword two times? Why not just
'tempVar2 = null'? Is this so that it doesn't go out to search the
scope tree to look for 'tempVar2' or for some other reason? An
detailed explanation with any relevant links would be greatly
appreciated.

Thanks and regards,
/~STS
Nov 21 '07 #7
On Nov 21, 1:13 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
Told ya you wouldn't believe me :)
I believe you. :-)
Is this so that it doesn't go out to search the
scope tree to look for 'tempVar2' or for some other reason?

That's a possibility, although it isn't in this case.
So you mean there are cases where this would be true? Any examples?
An detailed explanation with any relevant links would be greatly
appreciated.

Detailed explanation of the use of the var keyword? I am sure there is
one somewhere but I don't have a link to one. The simplest way to
remember it is that using var on a variable name in a function will
*never* alter a global variable. I always use it unless I explicitly
want to alter a global variable from within a function. The only other
time it can come into play is with inner functions and I never use them.
They give me too big a headache and I have never had a use for them.
Here is what I think of 'var', correct me if I am wrong.

'var' doesn't as such declare a variable. You don't need 'var' keyword
to *declare* variables or put them in the symbol tree in javascript.
It's just an indication to the scripting engine that 'please put this
variable in a scope which is local to this function. So saying:

var a = 10;
var a = 11;

doesn't actually create two variables or declare two variables but
refer to the same variable both the times which is 'a' which was
declared and defined using the statement 'a = 10'. Thus prepending the
variable name with 'var' separates it from the global namespace
(scope) (assuming we are not using 'with' in which case we would have
an additional scope).

Is this good enough?

Thanks and regards,
/~STS
Nov 22 '07 #8
ge**************@gmail.com said the following on 11/22/2007 1:37 AM:
On Nov 21, 1:13 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
>Told ya you wouldn't believe me :)
I believe you. :-)
>>Is this so that it doesn't go out to search the
scope tree to look for 'tempVar2' or for some other reason?
That's a possibility, although it isn't in this case.
So you mean there are cases where this would be true? Any examples?
A case where I wouldn't want it to go outside looking for it?

var tempVar = new Array();
//lots of entries in tempVar here

function something(){
tempVar = "...."
}

function somethingElse(){
alert('hey, where did my array go?')
}

A case where I would? The altering of a global variable, an array, or
several other things.
>>An detailed explanation with any relevant links would be greatly
appreciated.
Detailed explanation of the use of the var keyword? I am sure there is
one somewhere but I don't have a link to one. The simplest way to
remember it is that using var on a variable name in a function will
*never* alter a global variable. I always use it unless I explicitly
want to alter a global variable from within a function. The only other
time it can come into play is with inner functions and I never use them.
They give me too big a headache and I have never had a use for them.
Here is what I think of 'var', correct me if I am wrong.

'var' doesn't as such declare a variable. You don't need 'var' keyword
to *declare* variables or put them in the symbol tree in javascript.
It's just an indication to the scripting engine that 'please put this
variable in a scope which is local to this function. So saying:
var myArray = new Array();

Give it some thought :)
var a = 10;
var a = 11;

doesn't actually create two variables or declare two variables but
refer to the same variable both the times which is 'a' which was
declared and defined using the statement 'a = 10'. Thus prepending the
variable name with 'var' separates it from the global namespace
(scope) (assuming we are not using 'with' in which case we would have
an additional scope).

Is this good enough?
It isn't that simple though.

var someVar = "global variable";
function someFunction(){
var someVar = "local variable";
someVar = "modified in the function";
alert(window['someVar'])
}

Without testing, what will the alert say, and why? Then test it and see
if you were right without testing it :)
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 22 '07 #9
ge**************@gmail.com said the following on 11/22/2007 1:37 AM:
(assuming we are not using 'with' in which case we would have an additional scope).
Sidenote: IMNSHO[1], the 'with' operator is for people who don't know
any better. It is worse than eval. There are some weird creepy things
that go on with the 'with' operator.

[1] In My Not So Humble Opinion.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 22 '07 #10
On Nov 22, 12:46 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
A case where I wouldn't want it to go outside looking for it?

var tempVar = new Array();
//lots of entries in tempVar here

function something(){
tempVar = "...."

}

function somethingElse(){
alert('hey, where did my array go?')
}
Agreed. That is one of the reasons why I prefix globals with g_ and so
called constants with c_ .

A case where I would? The altering of a global variable, an array, or
several other things.
var myArray = new Array();

Give it some thought :)
Huh? I am confused.
It isn't that simple though.

var someVar = "global variable";
function someFunction(){
var someVar = "local variable";
someVar = "modified in the function";
alert(window['someVar'])
}

Without testing, what will the alert say, and why? Then test it and see
if you were right without testing it :)
I was able to guess the output without running it. The answer would be
'global variable' because AFAIK javascript starts searching for an
identifier from the closest scope to the most generic scope.

But then again, I might be wrong so please feel free to correct me. :-)
Nov 22 '07 #11
ge**************@gmail.com said the following on 11/22/2007 11:38 AM:
On Nov 22, 12:46 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
>A case where I wouldn't want it to go outside looking for it?

var tempVar = new Array();
//lots of entries in tempVar here

function something(){
tempVar = "...."

}

function somethingElse(){
alert('hey, where did my array go?')
}
Agreed. That is one of the reasons why I prefix globals with g_ and so
called constants with c_ .
l_ for local might be a better prefix.

<snip>
The answer would be 'global variable' because AFAIK javascript
starts searching for an identifier from the closest scope to the
most generic scope.
When looking it up, yes. When assigning to it, no. The use of the 'var'
keyword stops it from going "up" the scope chain.

I think there is an old thread somewhere where Richard Cornford
explained it better than I have. I just can't find it right now.
Searching the Google archives for "var" is, well, kinda pointless :)

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 23 '07 #12
On Nov 23, 11:46 am, Randy Webb <HikksNotAtH...@aol.comwrote:
[...]
When looking it up, yes. When assigning to it, no. The use of the 'var'
keyword stops it from going "up" the scope chain.

I think there is an old thread somewhere where Richard Cornford
explained it better than I have.
Here's one that includes - with -:

<URL:
http://groups.google.com.au/group/co...4a50b8acebecd5
>

--
Rob
Nov 23 '07 #13
obG wrote:
On Nov 22, 4:37 pm, getsanjay.sha...@gmail.com wrote:
>You don't need 'var' keyword to *declare* variables

Yes, you do. A variable without var isn't declared, it is created as
a property of the global object when the code executes. It is
equivalent (more or less) to:

window[variableName] = 'foo';
(Sigh. [psf 10.1] More or less; rather less. It should be

window['variableName'] = 'foo';

if that; `window' does not need to refer to the Global Object.)

IOW, a variable that is not declared with `var' is not a variable at all.
The identifier will refer to a property of an object O in the Scope Chain.
O is often the Global Object, which should be at the end of the Scope Chain,
but O does not need to. During identifier resolution, the first object that
reports to have such a property will be O.

In fact, O may be not a host object which makes this initialization-only
approach error-prone. It is known that in the MSHTML DOM IDs and names of
markup elements make up property names of an host object that is before the
Global Object in the Scope Chain which is why you get a runtime error if you
try write access to those properties.)

(Maybe I should create a template for that as people keep forgetting it even
though I post the reminder frequently.)
PointedEars
Nov 23 '07 #14
Thanks a lot Randy, Rob and Thomas for your enlightening. It would
take some time for all that to seep inside but when in doubt I know
where to turn to. :-)

Thanks and regards,
/~STS
Nov 25 '07 #15
Thomas 'PointedEars' Lahn wrote:
IOW, a variable that is not declared with `var' is not a variable at all.
The identifier will refer to a property of an object O in the Scope Chain.
O is often the Global Object, which should be at the end of the Scope Chain,
but O does not need to. During identifier resolution, the first object that
reports to have such a property will be O.

In fact, O may be not a host object which makes this initialization-only
approach error-prone. [...]
That should have either been "may not be a _native_ object" or "_may be_ a
host object_", of course. Sorry for causing confusion.
PointedEars
Nov 25 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Martin Vorbrodt | last post: by
5 posts views Thread by mike | last post: by
32 posts views Thread by David Mark | last post: by
5 posts views Thread by Immortal Nephi | last post: by
7 posts views Thread by ghulands | last post: by
53 posts views Thread by souporpower | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.