When we compare something to primitives in functions, are those primitives created and then garbage-collected or browsers do some optimizations on that? It seems such a basic case to me that I wonder if it makes sense to micro-optimize, e.g. assign compared primitive to a variable declared outside the function to avoid repetitive creation of the said primitive?
I often find myself writing:
function isGood(foo) {
return foo === 'good';
}
Does it make sense to pull out the 'good'
out of the function to avoid GC events if any?
var good = 'good';
function isGood(foo) {
return foo === good;
}