TL;DR -- What is the difference between these two methods of returning values, from the perspective of memory management? They are being managed within a game loop, and are modified thousands of times a second.
return { x: x1 + x2, y: y1 + y2, z: z1 + z2 } // VS predeclared.x = x1 + x2; predeclared.y = y1 + x2; predeclared.z = z1 + x2; return predeclared; Thanks for any insight!
Some context... In webGL / three.js programs in certain circumstances it is beneficial to pre-declare a dummy reference that will be used to temporarily hold an object every frame instead of creating a new object to assist with garbage collection. I'm pretty sure this is a common practice but just in case it isn't it looks like this:
let mesh; while( true ) { mesh = getNewMeshFrame( time ); renderStuff( mesh ); } I am unsure about the correct technique to be used in a situation like the following.
// With just ONE predeclared object and an object literal in the return statement function box() { let box = { x: 0, y: 0 }; this.transform = function( addX, addY ) { return { x: box.x + addX, y: box.y + addY }; } } // OR, with TWO predeclared objects function box() { let box = { x: 0, y: 0 }; let transformed = { x: 0, y: 0 }; this.transform = function( addX, addY ) { transformed.x = box.x + addX; transformed.y = box.y + addY; return transformed; } } I hope it's clear that the box object is static and must not change (it is a much more complex object IRL), so I need to keep it separate from its transform. Thank you!