2 ms·
That is eye opening, thanks. I can't believe how much faster new is. There's a glimmer of hope though. In Firefox 4.0.1 Object.create is almost as fast as new
by sjs 15y ago
That is eye opening, thanks. I can't believe how much faster new is.
There's a glimmer of hope though. In Firefox 4.0.1 Object.create is almost as fast as new Constructor. If other implementations caught up in this respect what are your feelings on new vs. Object.create?
- jashkenas 15y agoMy feelings are that "new" is really what you want. If you're thinking, "I want to make a new point at 20, 100", you probably want to write: new Point(20, 100); The problem in JS isn't the "new" keyword, it's how awkward and brittle prototypes are to create. You have to know to first define a constructor function (read "class object"), and then attach prototypal properties to it one at a time. If you intend to use the prototype chain for inheritance, you have to be even more careful, and bend over backwards to configure the prototype chain correctly, which requires the creation of a temporary intermediate function. See later in this thread: http://news.ycombinator.com/item?id=2595128 http://news.ycombinator.com/item?id=2595128 ... nasty stuff. Verbose, fragile, and confusing. This is why the ECMAScript committee is hard at work on proposals like these: http://wiki.ecmascript.org/doku.php?id=strawman:classes http://wiki.ecmascript.org/doku.php?id=strawman:classes http://wiki.ecmascript.org/doku.php?id=strawman:classes_as_inheritance_sugar http://wiki.ecmascript.org/doku.php?id=strawman:classes_as_i... http://wiki.ecmascript.org/doku.php?id=strawman:classes_as_sugar http://wiki.ecmascript.org/doku.php?id=strawman:classes_as_s... http://wiki.ecmascript.org/doku.php?id=strawman:classes_with_trait_composition http://wiki.ecmascript.org/doku.php?id=strawman:classes_with... http://wiki.ecmascript.org/doku.php?id=strawman:classes_without_trait_composition http://wiki.ecmascript.org/doku.php?id=strawman:classes_with...