Re: function pointers vs direct calling in javascript

Giganews Newsgroups
Subject: Re: function pointers vs direct calling in javascript
Posted by:  RobG (rgq…@iinet.net.au)
Date: Tue, 20 Nov 2007

On Nov 21, 9:24 am, Sampat <sampatdi.…@gmail.com> wrote:
> 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

Replies

In response to

function pointers vs direct calling in javascript posted by Sampat on Tue, 20 Nov 2007