Re: function pointers vs direct calling in javascript

Giganews Newsgroups
Subject: Re: function pointers vs direct calling in javascript
Posted by:  Peter Michaux (petermicha…@gmail.com)
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.

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

Replies

None

In response to

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