javascript - Can memory usage be reduced by avoiding var? -
let's have class myclass
function method()
going called a lot. of these implementations going more efficient?
function myclass() { this.method = function () { var number = 10; var boolean = true; var string = "string"; // }; } function myclass() { this.data = {}; this.method = function () { this.data.number = 10; this.data.boolean = true; this.data.string = "string"; // }; }
the first implementation creates new variables eligible garbage collection after execution of function since there's no reference them, great. however, if call function 3 times, there memory allocated total of 3 numbers, 3 booleans , 3 strings.
the second implementation doesn't create new variables, overwrites values of previous call of function instead. mean after 3 invocations of function, memory allocated 1 number, boolean , string, instead of 3? there 3 times less memory consumed?
function myclass() { this.method = function () { var number = 10; var boolean = true; var string = "string"; // }; }
first approach better variables method scope , collected gc once method execution complete. keep in mind if have keep these values class members method not work.
in second approach attaching these properties class reference. gc not collect until class instance referenced scope.
Comments
Post a Comment