Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Performance: inline- vs. prototype-defined methods ?

Reply
Thread Tools

Performance: inline- vs. prototype-defined methods ?

 
 
Gerald S
Guest
Posts: n/a
 
      01-31-2006
hi,

got a performance problem; situation is as described below (see "Version
Inline"), i've got a javascript class item with many methods defined
inside and with every call to "new item()" the js interpreter has to
look through all the method definitions (did a profile using Venkman)
which takes too much time for me ..

would it make a difference if i define these methods outside the
constructor via .prototype (see "Version Prototype") ??

Version Inline:

item = function () {
this.myMethod1 = function() { .. }
this.myMethod2 = function() { .. }
this.myMethod3 = function() { .. }
...
this.myMethod999 = function() { .. }
}


Version Prototype:

item = function () {

}

item.prototype.myMethod1 = function() { .. }
item.prototype.myMethod2 = function() { .. }
item.prototype.myMethod3 = function() { .. }
...
item.prototype.myMethod999 = function() { .. }



many thanks in advance !!

-
Gerald Stampfel

 
Reply With Quote
 
 
 
 
Gerald S
Guest
Posts: n/a
 
      01-31-2006
Duncan Booth wrote:
> Yes it will make a difference since you will only be setting up the methods
> once instead of every time you create an object, also if you have a lot of
> objects you will be using less memory so there may be a gain there. Calls
> to the methods may be slightly slower but that is unlikely to be
> noticeable.


ok, thanks!!

but if i do it the .prototype-way, there is another problem. consider
the following situation:


item = function () {
var privateVar;

function doInternalStuff();
}

item.prototype.myMethod1 = function() { .. }
item.prototype.myMethod2 = function() { .. }
item.prototype.myMethod3 = function() { .. }
...
item.prototype.myMethod999 = function() { .. }


two issues:
1) i can't access privateVar from my myMethodXXX methods (or can i?)
2) i can't call doInternalStuff() from my myMethodXXX methods

i could expose both to the public, but is there a way to keep them
private AND use them. any patterns ?

thanks again ..

-
Gerald Stampfel

 
Reply With Quote
 
 
 
 
Julian Turner
Guest
Posts: n/a
 
      01-31-2006

Gerald S wrote:

> Duncan Booth wrote:
> > Yes it will make a difference since you will only be setting up the methods
> > once instead of every time you create an object, also if you have a lot of
> > objects you will be using less memory so there may be a gain there. Calls
> > to the methods may be slightly slower but that is unlikely to be
> > noticeable.

>
> ok, thanks!!
>
> but if i do it the .prototype-way, there is another problem. consider
> the following situation:
>
>
> item = function () {
> var privateVar;
>
> function doInternalStuff();
> }
>
> item.prototype.myMethod1 = function() { .. }
> item.prototype.myMethod2 = function() { .. }
> item.prototype.myMethod3 = function() { .. }
> ...
> item.prototype.myMethod999 = function() { .. }
>
>
> two issues:
> 1) i can't access privateVar from my myMethodXXX methods (or can i?)
> 2) i can't call doInternalStuff() from my myMethodXXX methods
>
> i could expose both to the public, but is there a way to keep them
> private AND use them. any patterns ?


There are.

Search this group for posts from Richard Cornford: there have been
plenty of discussions on this subject.

Take a look at:-

<URL:http://www.litotes.demon.co.uk/js_info/private_static.html>
<URL:http://jibbering.com/faq/faq_notes/closures.html>
<URL:http://www.crockford.com/>

Regards

Julian

 
Reply With Quote
 
Julian Turner
Guest
Posts: n/a
 
      01-31-2006

Julian Turner wrote:

[snip]
> i could expose both to the public, but is there a way to keep them
> > private AND use them. any patterns ?

[snip]

Here is a quick example:-

var myObject=(function(){

var myPrivateVar=1;
var myPrivateFunction=function(){
alert("Hello from Private "+myPrivateVar);
}

function Constructor(){}

Constructor.prototype.alert=function()
{
myPrivateFunction();
myPrivateVar++;
}

return Constructor;
})();


var o1=new myObject();
var o2=new myObject();
o1.alert();
o2.alert();


Regards

Julian

 
Reply With Quote
 
Julian Turner
Guest
Posts: n/a
 
      02-01-2006

Duncan Booth wrote:

[snip]
> Except that in Gerald's original post 'privateVar' was a private instance
> variable and you've demonstrated how to make a private variable shared
> between all instances which is not the same thing at all.

[snip]

Fair point.

 
Reply With Quote
 
Julian Turner
Guest
Posts: n/a
 
      02-01-2006

Julian Turner wrote:

> Duncan Booth wrote:
>
> [snip]
> > Except that in Gerald's original post 'privateVar' was a private instance
> > variable and you've demonstrated how to make a private variable shared
> > between all instances which is not the same thing at all.

> [snip]
>
> Fair point.


[This is a duplicate message, as one posted earlier did not appear].

Putting my head above the parapet again, here is a very rough hack to
create the illusion of private instance variables. I am sure others
will have better ideas of course. It uses a count variable to uniquely
identify each instance, and a private object to store the private
variable.


var myObject=(function(){

var count=0;
var private={};

function Constructor(nPrivate){
this.id=count++;
private[this.id]=nPrivate;
}

Constructor.prototype.alert=function(){
alert(private[this.id]);
}

return Constructor;
})();


var o1=new myObject("private 1");
var o2=new myObject("private 2");
o1.alert();
o2.alert();

Regards

Julian

 
Reply With Quote
 
Julian Turner
Guest
Posts: n/a
 
      02-01-2006

Julian Turner wrote:

> Duncan Booth wrote:
>
> [snip]
> > Except that in Gerald's original post 'privateVar' was a private instance
> > variable and you've demonstrated how to make a private variable shared
> > between all instances which is not the same thing at all.

> [snip]


Putting my head above the parapet again, here is some kind of hack (by
no means perfect) which includes a counter to count each instance
created and uses a global private object to store private instance
variables:-

var myObject=(function(){

var count=0;
var private={};

function Constructor(nPrivate)
{
this.id=count++;
private[this.id]=nPrivate;
}

Constructor.prototype.alert=function()
{
alert(private[this.id]);
}

return Constructor;
})();


var o1=new myObject("Private Value 1");
var o2=new myObject("Private Value 2");
o1.alert();
o2.alert();

Regards

Julian

 
Reply With Quote
 
Julian Turner
Guest
Posts: n/a
 
      02-01-2006

Duncan Booth wrote:

[snip]
> a) I guess if you tested it you used IE. It will give you a syntax error on
> Firefox or anything close to a conforming ecmascript implementation.


Right. I will give it a try in Firefox. Did you have a record of the
syntax error it gave?

[snip]
> b) It leaks memory. The private values are never released.


Good point. It could require some form of garbage collection for the
private object.

If at first you don't succeed...

Regards

Julian

 
Reply With Quote
 
Julian Turner
Guest
Posts: n/a
 
      02-01-2006

Duncan Booth wrote:

> The actual message is kind of irrelevant (and not at all informative).
> 'private' is a reserved word so you can't use it as a variable name, all
> you have to do is rename it.


Doh. Of course.

Here are couple of other attempts to play with:-

1. Priviledged Accessor (so named by Douglas Crockford)

var myObject=(function(){

function Constructor(v)
{
var privateInstance=v;

this.getPrivateInstance=function(){
return privateInstance;
}
}

Constructor.prototype.alert=function()
{
alert("Private Instance "+this.getPrivateInstance());
}

return Constructor;
})();


2. Private Instance is copied to common private variable for use. I
am sure this has lots of other potential problems, apart from lacking
elegance.

var myObject=(function(){

var privateHolder;

function getPrivate()
{
return privateHolder;
}

function Constructor(v)
{
var vPrivate=v;

this.openPrivate=function(){
privateHolder=v;
}

this.closePrivate=function(){
privateHolder=null;
}
}

Constructor.prototype.alert=function()
{
this.openPrivate();

alert(getPrivate());

this.closePrivate();
}

return Constructor;
})();


Regards

Julian

 
Reply With Quote
 
Gerald S
Guest
Posts: n/a
 
      02-03-2006

thanks Julian and Duncan for you solutions, didn't know you could do
such crazy things with this language!!

in my case the problem is i want to distinguish between private <->
public AND define the methods for an object outside of the consructor (
because [as stated in the first post of this thread] i have to create
many instances of this class.

but i guess after reading your solutions and trying a few things this is
not so easy (if not impossible). i'm not into javascript for very long,
my mind is still thinking in java-terms ..

the solution for me is to give up on seperating private and public, just
use some naming conventions for pseudo-private things and make
everything public like this:

item = function () {
this._privateFieldA = 0;
this._privateFieldB = 0;
this._privateFieldC = 0;
..
}

item.prototype.manipulateA = function() {
this._privateFieldA++;
}

var myObj = new item();

and using the object like myObj.manipulateA() [even if it could be done
using myObj._privateFieldA++]


thanks for you effort guys!

-
Gerald Stampfel

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Top-Up Methods - Finarea (Voipcheap, internetcalls, etc.) et. al., VOIP Services Question - Top-up Methods News Reader UK VOIP 0 04-10-2006 02:41 PM
Why Petshop Changed all static methods to instance methods when upgrading from version 3.0 to version 3.1? Neo ASP .Net 1 01-07-2005 01:46 AM
Derived methods hiding inherited methods Tron Thomas C++ 10 11-10-2004 09:32 AM
Re: Templates and friends and template methods with private methods. Buster Copley C++ 5 07-07-2003 12:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57