using a variable to replace “new Rect( )” is better?
1 Answer
1I am unsure what you are asking, but I’ll answer the first few things that seem to be similar to what you are looking for.
is "var r = new Rect( )" better than calling "new Rect( )"
every time I need to use a Rect?
Yes, it is better to store your value in a var. It costs a small amount of memory, but the processor time it saves over several frames is worth it in the long run. Also, it is significantly easier to use in the future if you store something into a var. It’s better to store too many things in vars than too few.
is there a difference between "Rect( )" and "new Rect( )"?
In JavaScript, no. In every other programming language in existence, yes. You can just do the statement without the “new” in JS to save time, but remeber to add in the “new” if you ever need to change languages. JS adds in the new for you automatically so you don’t have to type it.
is it better to change a single value of a Rect "r.xMin = newValue" or is it
better to create a new Rect with "r = new Rect( )"
If you are just changing one value of the Rect then it can be easier to just replace a single value. If you are changing two or more values of a Rect at once it will probably be easier to create a new Rect(). There is some time spent creating the new Rect, but as long as it is a one-off occurrence, it will not affect performance. Slow, working, legible code is better than fast, hard to change, illegible code.