After testing which is more performant (dist between vector3’s or vector2’s), I found that calculating between vector3’s is much more performant.
I’m confused why? Since there’s another axis, meaning more numbers to calculate, shouldnt vector2’s be faster? Is something with my benchmark code wrong?
task.wait(3)
-- Vector3
local a = Vector3.new(34.654, 546.654, 546.654)
local b = Vector3.new(54365.65464, 654.645, 546.654)
local function dist()
for i = 1, 8 do
local dist = (a - b).Magnitude
local start = os.clock()
for i = 1, 4_000_000 do
dist()
print(os.clock() - start)
-----------
task.wait(.25)
-----------
-- Vector2 (why is this less performant?)
local a = Vector2.new(34.654, 546.654)
local b = Vector2.new(54365.65464, 654.645)
local function dist2()
for i = 1, 8 do
local dist = (a - b).Magnitude
local start = os.clock()
for i = 1, 4_000_000 do
dist2()
print(os.clock() - start)
Hey developers,
As we continue our work on Luau VM performance, one of the items we wanted to address was performance of Vector3 operations.
Vector3s used to be heap-allocated, meaning that memory allocation took place for every vector, and only de-allocated by garbage collection - which was having a negative impact on performance. This particularly impacts code that creates a lot of short-lived temporary vectors, which could trigger a lot of garbage collection steps. Another important area is…