Performance when creating new Vector objects

Hello Babylon team,

to improve performance I am trying to avoid creating new Vectors as much as possible.

I have this use case where I want to draw on a plane and I call getTextureCoordinates on every tick to check where to draw.
This method returns a new Vector2 every time I call the method. And as I looked closer into the method I noticed that it is actually creating 4 new Vector2 on every call.

Link to method

Do you think it makes sense to improve the method by reusing the Vectors inside the method (e.g. by using a closure).
And do you think it would be a good approach to let users pass a target vector to the method, which gets altered by the method rather than returning a new Vector2 on every call.

Thank you!

1 Like

Honestly I can’t imagine that process being that expensive but I could be wrong! . would love to hear others thoughts but if you feel this is a change to be made I’d say go for it and open a PR :smiley:

A getTextureCoordinatesToRef(uv: Vector2): boolean would probably be a good idea and would avoid GC if the method is used often.

However, no need for closures, you can use TmpVectors.Vector2[0|1|2|...] for temporary vectors.

1 Like