473,769 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4286
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.javas cript 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...@gm ail.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.childOb j.method();
then you can assign a reference to a local variable:

var x = baseObj.childOb j.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.childOb j.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...@gm ail.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...@gm ail.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.javas cript 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.javas cript 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.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 22 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2032
by: nick | last post by:
I have the following code: var ocevent = function(v) { alert('javascript event: u clicked '+v); return false; };
2
9801
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 in a link. that function can have parameters for URL and window name passed to it. This works peachy in Firefox (1.0). With IE 6 (6.0.29) on two separate computers, I get an "onject expected" error. Going to the MS-based debugger just tells me...
10
7315
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
4643
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
3528
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') This should work as well, but I am not sure how well it degrades in older browsers. I think there are issues with functions created in another context (eg frame) as well.
13
7814
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 statement used in calling the function validateForm rather than being included inside the function? Thanks in advance,
5
3653
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 10 member functions. Can switch be replaced to member function pointer array? Please provide me an example of source code to show smart pointer inside class. Thanks....
7
3829
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 event); so I declared a function pointer like typedef void (CFObject::*CFEventHandler)(CFEvent *theEvent);
53
8416
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 language="javascript" type="text/javascript">
0
9416
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9850
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7396
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.