473,320 Members | 2,098 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,320 software developers and data experts.

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

Similar topics

5
by: nick | last post by:
I have the following code: var ocevent = function(v) { alert('javascript event: u clicked '+v); return false; };
2
by: Chuck Martin | last post by:
I am having a most frustrating problem that references, web searches, and other resources are no help so far in solving. Basically, I'm trying to design a pop-up window to be called with a funciton...
10
by: Martin Vorbrodt | last post by:
Example code in one of my books intrigues me: class B { public: B* Clone() const { B* p = DoClone(); assert(typeid(*p) == typeid(*this)); return p; }
5
by: mike | last post by:
If I have a document like: <script> function mike_test() {alert('hi');} </script> <iframe src="blank.html" id="my_iframe1"> </iframe> and in blank.html I have:
32
by: David Mark | last post by:
I've got a collection of functions that accept a function or object (paired with a method name) as a callback. For the longest time I have relied on this test. (typeof cb == 'function') ...
13
by: Steve | last post by:
On page 392 of "Javascript the definitive guide" a function is called like this:- <form action="processform.cgi" onsubmit="return validateForm();"> Why, in this instance, is the return...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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.