19 ms·
> If you need to write performance critical JavaScript, then allocating new objects will be just as slow as creating new closures. No, it won't. This code:
by Touche 11y ago
> If you need to write performance critical JavaScript, then allocating new objects will be just as slow as creating new closures.
No, it won't. This code:
function createThing() {
return {
doSomething: function(){}
};
}
Performs much worse (at scale) than this code:
new Thing();
And it consumes a lot more memory since Thing.prototype.doSomething is 1 function in memory and the former is N functions.
- jeremiep 11y agoI try to avoid OOP in my JavaScript code like the plague, I'd probably write it like this: function doSomething(obj){} var objs = [{},{},{}]; objs.forEach(doSomething); If I need late binding I'll use something like clojure's multi-methods. Both closures and prototypes aren't needed for this :)