Search code examples
performancetypescriptgame-loop

Names vs anonymous functions in TypeScript


Which one is better in terms of performance and performance only?

1)

function GameLoop()
{
    // Some heavy calculations
    requestAnimationFrame(GameLoop);     
}
requestAnimationFrame(GameLoop);

2)

function GameLoop()
{
    // Some heavy calculations
    requestAnimationFrame(function()
    {
       GameLoop();
    });
}
requestAnimationFrame(GameLoop);

Solution

  • performance and performance only

    1

    Because in 2 you are creating a new function on each iteration.

    More : in modern vm's they will realize the 2 is creating a same function again and again and will get optimised so the long term performance impact will be minimal.