-
-
Save petermichaux/2552304 to your computer and use it in GitHub Desktop.
var todoViews = todoModels.map(function(todoModel) { | |
return new TodoView(todoModel); | |
}); | |
// if Function.prototype.new is defined or TodoView.new is defined using "this" | |
var todoViews = todoModels.map(TodoView.new, TodoView); | |
// if Todo.new is defined without using "this" | |
var todoViews = todoModels.map(TodoView.new); | |
FTR: I'm aware of the issues with Object.create
, and a simple solution is to use a constructor-based shim (ignore the descriptor argument). I don't think a poorly performing native Object.create
is a good argument against basing a design on just objects and Object.create
-like construction.
@cjohansen I had noticeable issues on V8 engine (which implements Object.create
natively), what would you propose in that case? Also it's not that better for shims, see: http://jsperf.com/object-create-vs-crockford-vs-jorge-vs-constructor/31
It's not great argument but sometimes poor performance asks for refactoring back to new
(I've gone that path in one of the projects).
@medikoo On Firefox, Object.create comes out the fastest. V8 obviously has some insane optimization for the bare constructor use (as mentioned before). I don't really worry about it...
@cjohansen on Firefox performance of Object.create vs new is not really different (it's slightly faster on benchmark that's it), on V8 it is noticeable in real world. Hopefully one day V8 will have it fixed. I also do not tend to worry about things prematurely ;)
Object.create
unfortunately is magnitudes slower thannew
in some engines. It's rather an engines (not specification) issue, so hopefully it'll get better. Anyway for now I would useObject.create
only for prototype extensions (e.g. Animal to Dog), and stick tonew
when creating object instances (e.g. Dog to sparky).