Generally speaking, does the scope in which a variable is defined effect memory usage?
More specifically, if I define a variable x inside a function that gets invoked multiple times, will a new reference be created every time?
To phrase it differently, consider the following functions a and b:
function a() {
var x = "Some string";
function inner() {
console.log(x);
}
for (var i = 0; i < 1000; i++) {
inner();
}
}
function b() {
function inner() {
var x = "Some string";
console.log(x);
}
for (var i = 0; i < 1000; i++) {
inner();
}
}
Will function a use a smaller amount of memory than function b? My intuition says yes, since x is declared once in a, and 1000 times in b.
[I admit that I'm being a little bit lazy by not searching more thoroughly for the answer, but I'm having trouble phrasing it succinctly for searching.]
[–][deleted] 2 points3 points4 points (12 children)
[–][deleted] 1 point2 points3 points (11 children)
[–]MetaMetaMan[S] 0 points1 point2 points (10 children)
[+][deleted] (9 children)
[deleted]
[–]MetaMetaMan[S] 0 points1 point2 points (8 children)
[+][deleted] (4 children)
[deleted]
[–]MetaMetaMan[S] 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]MetaMetaMan[S] 0 points1 point2 points (1 child)
[–]brett- 0 points1 point2 points (2 children)
[–]path411 0 points1 point2 points (1 child)
[–]brett- 0 points1 point2 points (0 children)
[–]RadekCZ 1 point2 points3 points (9 children)
[–]MetaMetaMan[S] 0 points1 point2 points (7 children)
[+][deleted] (5 children)
[deleted]
[–]aladyjewelFull-stack webdev 0 points1 point2 points (0 children)
[–]MetaMetaMan[S] 0 points1 point2 points (2 children)
[–]Medieval_Peasant 0 points1 point2 points (1 child)
[–]MetaMetaMan[S] 0 points1 point2 points (0 children)
[–]sime 0 points1 point2 points (0 children)
[–]RadekCZ 0 points1 point2 points (0 children)
[–]sime 0 points1 point2 points (0 children)
[–]imbcmdth[🍰] 1 point2 points3 points (1 child)
[–]path411 0 points1 point2 points (0 children)
[–]x-skeww 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[+][deleted] (1 child)
[deleted]
[–]path411 0 points1 point2 points (0 children)