> There is overhead to create and destroy a variable 100 times.

I tested this, and it seems that the variable memory location (on the stack) is 
reused; i.e. the space is reserved for it once on the stack.

Note: Keep in mind that string objects store a pointer to the actual string 
data on the heap. So while the string metadata is stored on the stack (and 
reused in this case), the different string values are allocated/destroyed as 
you assign new strings to the variable.

As for best practices, it really depends on what you're doing with such 
variables. In the example you provided it's completely fine to keep it that 
way. If you need the variable to have an initial value, declare it just before 
the loop.

Reply via email to