Yes, the garbage collector will have a string object containing "Some string" to get rid of it. And, answering your question, this line assignment will do the job for the GC.
Since strings are immutable and used a lot, the JS engine has a pretty efficient way to deal with them. You should not notice any pauses from the garbage collecting several lines. The garbage collector has work to do all the time in the normal course of javascript programming. This is how it should work.
If you observe pauses from the GC, I rather doubt it from a few lines. Most likely, a much bigger problem is occurring. Either you have thousands of objects that need a GC, or a very difficult task for the GC. We could not really reflect on this without studying the general code.
This should not cause concern if you are not making some huge loop and not dealing with tens of thousands of objects. In this case, you can program a little more carefully to minimize the number of intermediate objects that were created. But, if this level of objects is missing, you should first get clear, reliable code, and then optimize performance only when something has shown you that there is a performance problem that needs to be worried about.
jfriend00
source share