tag:blogger.com,1999:blog-34454975.post1745520054313783406..comments2023-06-28T16:58:41.189+02:00Comments on Web Reflection: new Constructor VS Object.createAndrea Giammarchihttp://www.blogger.com/profile/16277820774810688474noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-34454975.post-77022162238087077062010-03-29T20:09:08.283+02:002010-03-29T20:09:08.283+02:00Thanks Andrea - this is interesting
A lot of the ...Thanks Andrea - this is interesting<br /><br />A lot of the headache with inheritance in JS is that ECMA never created a standard for it - so developers were left in their own and now we have code wars...and everyone BASHES new which is a shame because it is transparent and performs the best and you need to know Javascript to use it properly.<br /><br />So finally ECMA have a standard but clearly the probelm is not solvedAngus Crollhttps://www.blogger.com/profile/08977269449096492048noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-49720957763859636692010-03-29T12:45:20.116+02:002010-03-29T12:45:20.116+02:00the literal notation is surely the fastest option ...the literal notation is surely the fastest option but as you said better patterns mke life easier and a centralized "place" to create a Point is a better pattern, imho, so that we can add more to Points, if necessary, when necessary, and in one place.<br /><br />About micro optimizations, as I have said, more we move forward with complex applications, more we have to deal with portable/mobile devices where power computation is crap.<br /><br />If you have to deal with ARM processors on daily basis, you'll notice a massive boost using best practices, same is for Netbook and these CPUs (Atom and others) and this is something developers keep forgetting, testing with their supadupa hardware accelerated PCs/Laptops creating *monsters* for users with devices theoretically made to surf the net and nothing else.<br /><br />The key is to find a good compromise, after being aware about these issues/problems/techniquesAndrea Giammarchihttps://www.blogger.com/profile/16277820774810688474noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-65280169566839845942010-03-29T12:27:55.174+02:002010-03-29T12:27:55.174+02:00Andrea, the benchmarks are great and interresting,...Andrea, the benchmarks are great and interresting, though I do feel some emphasis must be made that programmers should avoid creating lots of objects in the first place and reuse already created object through delegation.<br /><br />With that in mind, performance is less of an issue and readability and maintenance can be prioritized. Also, the best way to create 'simple' objects is through literal object notation:<br /><br />var myPoint = {x:1,y:1,z:1};<br /><br />It does not need to be a class or prototype really ;o)<br /><br />Like I said, the benchmarks are really interresting, but we have a long history in optimizing JS because we had to. However we are already at the point writing smarter architectures (design patterns) offers a higher gain in performance then micro optimisation.<br /><br />Very curious to hear your opinion on this ;O) as I know you are a micro optimiser galore =PNetBenhttp://www.netben.nlnoreply@blogger.comtag:blogger.com,1999:blog-34454975.post-60655347374367638212010-03-29T00:58:13.658+02:002010-03-29T00:58:13.658+02:00@itpastorn I do believe once you implement somethi...@itpastorn I do believe once you implement something, something should be implemented properly, but maybe there is no JIT optimization for such operation, still weird but a bug must be probably fired.<br /><br />@Dmitry there is still a consistent gap between the ES3 way and the ES5 one. I will test Rhino and others as well via my Atom, but the point is: think about Object.create shims, if the native is still slower, how much a shim will impact performances?<br />In few words, where performances matters, and we don't need Object.create features, it's not worth it at all to prefer latter pattern rather than good old <i>new Constructor</i>.<br />My attempt was the one to underline all differences you described indeed, I would rather define properties directly to the prototype avoiding create patern where necessary.<br />As we all know, more we move forward, less power computation we have to deal with (mobile, netbook, etc) so these considerations must be done.<br /><br />I know about static, nice one nothing happened in my tests :DAndrea Giammarchihttps://www.blogger.com/profile/16277820774810688474noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-32415094369466427102010-03-28T22:19:47.198+02:002010-03-28T22:19:47.198+02:00Thanks for this analysis, but it also depends on i...Thanks for this analysis, but it also depends on implementation; e.g. in Rhino I have the following results:<br /><br />ms: 1078<br />ms: 1875<br />ms: 1094<br /><br />And in other call already the following (the first approach is less effective):<br /><br />ms: 1109<br />ms: 1891<br />ms: 1094<br /><br />The second approach should be less effective also because it should resolve "Object.create" accessor: first "Object" name, then find ".create" name in it. After that -- to resolve "proto" name also. Of course, creating a new properties-object on each iteration also makes performance issue.<br /><br />The third case also should resolve three names ("create", "proto" and "fixed"), but there is improvement because of one instance of properties-object.<br /><br />Theoretically, but the ECMA-262-5, [[Construct]] which is called by <i>new</i> should work faster if simply to compare algorithms.<br /><br /><i>Object.create</i> (15.2.3.5) should first do all work of <i>new Object</i> (15.2.2.1) -- with all checks, etc. After that again set [[Prototype]] property (which means it do it twice in contrast with <i>new</i>) and then call <i>Object.defineProperties</i> (15.2.3.7) which also makes own checks, conversions, iteration over properties and so on -- again -- in contrast with a function call to initialize newly created object, though, a function call is also not so cheep operation.<br /><br />P.S.: <i>static</i> is future reserved word, should be a SyntaxError.<br /><br />Dmitry.Dmitry A. Soshnikovhttp://dmitrysoshnikov.comnoreply@blogger.comtag:blogger.com,1999:blog-34454975.post-40141807255151712742010-03-28T21:36:05.122+02:002010-03-28T21:36:05.122+02:00Perhaps its a bit premature to test the performanc...Perhaps its a bit premature to test the performance for ES5. But filing a bug for Chrome about the deficience might be a good idea.Lars Gunther (itpastorn)https://www.blogger.com/profile/11544012919049072827noreply@blogger.com