4 ms·
Well, although simpler and less error prone, the 'self' method is quite less optimal than the prototype way. A quick test reveals that the first technique consu
by pabloPXL 15y ago
Well, although simpler and less error prone, the 'self' method is quite less optimal than the prototype way. A quick test reveals that the first technique consumes much more memory (100000 new instances pushed into an array): http://imgur.com/YVkny http://imgur.com/YVkny
self:
var a = [];
function b(){ var c = {}; c.d = function(){}; return c };
for (var e = 0, f = 100000; e < f; e++) a.push(new b)
prototype:
var a = [];
function b(){ return this };
b.prototype.c = function(){};
for (var d = 0, e = 100000; d < e; d++) a.push(new b)