473,386 Members | 1,743 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,386 software developers and data experts.

Performance: inline- vs. prototype-defined methods ?

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
gs*******@sedisys.com
Jan 31 '06 #1
14 1819
Gerald S wrote:
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") ??

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.
Jan 31 '06 #2
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
gs*******@sedisys.com
Jan 31 '06 #3
Gerald S wrote:
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 ?

Javascript doesn't really support private variables. Your best action is
simply to use some convention to indicate that they are private (e.g. a
leading underscore on the name) and then not access them from outside the
appropriate functions.
Jan 31 '06 #4

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

Jan 31 '06 #5

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

Jan 31 '06 #6
Julian Turner wrote:

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();

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.
Feb 1 '06 #7

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.

Feb 1 '06 #8

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

Feb 1 '06 #9
Julian Turner wrote:
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();


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.

b) It leaks memory. The private values are never released.
Feb 1 '06 #10

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

Feb 1 '06 #11

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

Feb 1 '06 #12
Julian Turner wrote:

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?


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.

Feb 1 '06 #13

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

Feb 1 '06 #14

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
gs*******@sedisys.com
Feb 3 '06 #15

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

Similar topics

1
by: Yuri Victorovich | last post by:
I have simple program which computes internal product on large vector. Whan the element of vector is "int" performance is about 70% better than when I use class with single field "int" in it and...
12
by: Fred | last post by:
Has anyone a link or any information comparing c and c++ as far as execution speed is concerned? Signal Processing algorithms would be welcome... Thanks Fred
0
by: Alex Vinokur | last post by:
=================================== ------------- Sorting ------------- Comparative performance measurement =================================== Testsuite : Comparing Function Objects to...
1
by: Per Nordlöw | last post by:
Hi all I am using the boost::array template class trying to generalize my handcrafted vector specialization for dimensions 2 (class vec2), 3 (class vec3) etc. As performance is of greatest...
5
by: Per | last post by:
Hi I am currently working on a mathlib and I want maximum performance (speed). If I delcare a function in the mathlib inline will the user apps take advantage of that or will it be called as...
9
by: GJ | last post by:
The performance can be DEGRADED by making a function "inline". Is it TRUE in any case ? -- GJ
2
by: Showjumper | last post by:
Is there a peformance hit when using inline code? As an example, lets i store a user's preferences for backgtound color of a page. When he/she logs in, i stick the color value into their session...
14
by: Sugandh Jain | last post by:
Hi, The warning from Microsoft.Performance Code Analysis check that, its not required to initialize numeric variables to zero, boolean to false and object to null is a good one because CLR does...
5
by: Sarath | last post by:
Dear All, I've a class which has lot of Get/Set functions. Since most of the data members are of same type (e.g float), I'd like to implement it with the help of lookup table ( probably will be...
2
by: Ken Camann | last post by:
Hey everyone. First of all let me say that I know that the C++ standard never makes any guarantees about compiler implementation and thus about the performance of any language feature. That...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.