tag:blogger.com,1999:blog-34454975.post4727311844180714797..comments2023-06-28T16:58:41.189+02:00Comments on Web Reflection: On JavaScript Inheritance Performance - One Step BackAndrea Giammarchihttp://www.blogger.com/profile/16277820774810688474noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-34454975.post-63539618047989382732009-03-02T08:18:00.000+01:002009-03-02T08:18:00.000+01:00OT: Andr3a, did you use the (new Date).getTime() w...OT: <BR/>Andr3a, did you use the (new Date).getTime() while you're benchmarking on IE?<BR/><BR/>Because is inaccurate:<BR/>http://ejohn.org/blog/accuracy-of-javascript-time/<BR/>you should make your benchmarking (only in IE) in something like this:<BR/>http://www.codeplex.com/IEJst<BR/><BR/>sorry fot the OT, but I think is important ;)kentaromiurahttps://www.blogger.com/profile/01628828429767241919noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-18884938656058691522009-03-02T04:09:00.000+01:002009-03-02T04:09:00.000+01:00Hi Andrea,Had another thought...Why not get rid of...Hi Andrea,<BR/><BR/>Had another thought...<BR/><BR/>Why not get rid of the overhead involved in copying over properties to the prototype and checking for the enumeration bug?<BR/><BR/>I've added a j3Class2 which does this, and a new test "Create Instantiate Call". If you check the following links you'll see there is a big difference between the two:<BR/><BR/>http://tinyurl.com/bloe2q<BR/>http://tinyurl.com/bzlax6<BR/><BR/>Thanks,<BR/><BR/>Reiss :)reisshttps://www.blogger.com/profile/10069790265503685936noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-64492368316102171542009-03-02T00:18:00.000+01:002009-03-02T00:18:00.000+01:00Hi Andrea,I'm not quite sure what you mean by:"deb...Hi Andrea,<BR/><BR/>I'm not quite sure what you mean by:<BR/><BR/>"debug with a method which contains multiple parents could be hard and for every extended method you need to rewrap the prototype"<BR/><BR/>Do you have an example of the above?<BR/><BR/>"(and yes, in your test my solution wins and respects hidden methods in IE as well)"<BR/><BR/>To be honest there is hardly anything in it in terms of speed, and what is lost, is more than made up for in code maintainability, readability and bytes (which I thought you'd like being a "byte maniac" lol).<BR/><BR/>"Try to consider that prototypes could be added/changed runtime as well, I know cases are few but if you trap the parent you cannot add another method later able to use that parent"<BR/><BR/>The point of a class is to define an object, so I'm not sure when you would want to add to that definition after it has been defined? And if you did want to add to it what would be wrong with doing this?<BR/><BR/>Constructor.prototype.newMethod = (function(base){<BR/> return function(){<BR/> //some stuff<BR/> //parent call<BR/> base.newMethod.call(this)<BR/> }<BR/>})(Parent.prototype);<BR/><BR/>"The good thing is that my suggestion could make function portables"<BR/><BR/>In what way are they more portable, the parent name is hard coded, which I thought would make them less portable?<BR/><BR/>Thanks,<BR/><BR/>Reiss :)reisshttps://www.blogger.com/profile/10069790265503685936noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-35109812699192307202009-03-01T11:27:00.000+01:002009-03-01T11:27:00.000+01:00@Reiss, I read more carefully your idea and I thin...@Reiss, I read more carefully your idea and I think it is generally convenient.<BR/>The only problem I can spot is when you need to re-use an extended prototype with another class. Since you trap the parent, debug with a method which contains multiple parents could be hard and for every extended method you need to rewrap the prototype. I trap a method but it is an extreme performances case and nothing else (and yes, in your test my solution wins and respects hidden methods in IE as well).<BR/>What I tried to say in this "Step Back" post is that explicit calls are much better than whatever injected or static shortcut solution we adopt.<BR/>Try to consider that prototypes could be added/changed runtime as well, I know cases are few but if you trap the parent you cannot add another method later able to use that parent. As summary, explicit calls, if we need extreme performances for ONE METHOD (not every one) then we could wrap the prototype method with a closure that contains <B>one or more</B> parent methods. The good thing is that my suggestion could make function portables and we can add even methods which have not been inherited.<BR/><BR/>@Covex, your solution seems to be fast as mine is, please do not rely in 1 to 10% perfromances with JavaScript because there is no benchmark so accurate. call against apply, well, it is all about convenience but when arguments are exactly the same, I usually prefer apply :-)Andrea Giammarchihttps://www.blogger.com/profile/16277820774810688474noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-24134584144438491092009-03-01T01:10:00.000+01:002009-03-01T01:10:00.000+01:00@Covex - Hi, your test uses the old version on j3C...@Covex - Hi, your test uses the old version on j3Class and doesn't give accurate results. I've uploaded the same test but with the newer version:<BR/><BR/>http://www.projectcss.net/inheritance/joos.html<BR/><BR/>Thanks,<BR/><BR/>Reiss :)reisshttps://www.blogger.com/profile/10069790265503685936noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-76777689701218457782009-02-28T21:02:00.000+01:002009-02-28T21:02:00.000+01:00Hi Andrea,"I did not suggest to wrap everything"I ...Hi Andrea,<BR/><BR/>"I did not suggest to wrap everything"<BR/><BR/>I never said you suggested to wrap everything. I said:<BR/><BR/>"you are suggesting that every method that requires a parent call should be wrapped in a closure?"<BR/><BR/>I think if one is going to use this type of pseudo class syntax then it's important to be consistent with it. So if you say<BR/><BR/>"// WebReflection parent method call in closure"<BR/><BR/>Then that pattern should be used for anything that calls its parent, hence why I did this in the syntax link above. Also using:<BR/><BR/>"Human.apply(this, arguments)"<BR/><BR/>Seems to be a lot slower than using ".call" with explicit arguments. I tested this while doing j3Class and there was a definite difference in speed.<BR/><BR/>Also by hard coding the Parent class name in the child class spec, you could end up with a lot of changes if you decided to rename the parent class e.g. from Human to HomoSapien.<BR/><BR/>Reiss :)reisshttps://www.blogger.com/profile/10069790265503685936noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-18663969256844084852009-02-28T20:24:00.000+01:002009-02-28T20:24:00.000+01:00Reiss, wait a moment ... I did not suggest to wrap...Reiss, wait a moment ... I did not suggest to wrap everything, only and only if necessary those methods whose would like to call a parent one, as is in my example (toString).<BR/>Accordingly, I would have written your Syntax example like this:<BR/>var Fighter = wr.extend(<BR/> function(sName, sHairColour, sEyeColour,iHeight, iWeight){<BR/> Human.apply(this, arguments);<BR/> },<BR/> Human,<BR/> {<BR/> className: 'Fighter', <BR/> chain: (function(chain) { <BR/> return function(){<BR/> return chain.call(this) + ' Chain1 Fighter(' + this.className + ') >'; <BR/> } <BR/> })(Human.prototype.chain)<BR/> }<BR/>);<BR/>which I suppose has best perfromances.Andrea Giammarchihttps://www.blogger.com/profile/16277820774810688474noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-70952263714792297442009-02-28T20:08:00.000+01:002009-02-28T20:08:00.000+01:00Hi Andrea,This is basically the same as I proposed...Hi Andrea,<BR/><BR/>This is basically the same as I proposed in the comments on your other post:<BR/><BR/>http://webreflection.blogspot.com/2009/02/on-javascript-inheritance-performance.html<BR/><BR/>I'm a bit disappointed you haven't included j3Class or JooS in your tests. I've created the tests for wr Class over at:<BR/><BR/>http://www.projectcss.net/inheritance/<BR/><BR/>To be honest it was a bit of a chore. If I have read your post correctly you are suggesting that every method that requires a parent call should be wrapped in a closure? This leads to extremely verbose code having to type the method name 4 times for each function. Parent constructor calling threw me a bit at first as your implementation uses an anonymous function and so the way in which it is called isn't consistent with the rest of the parent calling methods (slightly).<BR/><BR/>Having ran the tests on the link above wr Class method calling is no faster than j3Class so I wonder why you have opted to type Parent.prototype.methodName everytime? Here is a quick comparison:<BR/><BR/>http://www.projectcss.net/inheritance/syntax.html<BR/><BR/>This is by no means a dig at your implementation, so please don't take it that way. I'm just curious why you have chosen to do it this way?<BR/><BR/>Thanks,<BR/><BR/>Reiss :)reisshttps://www.blogger.com/profile/10069790265503685936noreply@blogger.com