# How to reduce the total time spent checking against my world bounds?

I'm making a game engine and currently I use a function for checking a new position after movement to prevent game objects from escaping the world:

const boundary = function (min, max) {
return Math.min( Math.max(this, min), max );
};


I've noticed that it consumes around 9-10% of my game loop (testing with 4500 objects). Is there a better way to handle this? Do I generally need to make my world finite?

To clear this up, this is not an object-object collision detection question. For that I have a grid, and this grid is based on the size of the world, which is why I check that objects don't escape the world size limits.

• You are testing with 4500 objects and it consumed around 9-10% of the game loop. Why is 10% of the game loop more than you would expect? Are 4500 objects the typical amount you expect out of testing? I think, in general, you should be against optimizing early, or optimizing for fabricated tests. Build an engine, then see what its bottlenecks are down during normal operation, then target your optimizations to where they are needed. "Premature optimization is the root of all evil." - Donald Knuth. – Garrett Gutierrez Sep 28 '18 at 18:15
• @ Garrett Gutierrez That's a very good point, but to answer your question, I'm not currently developing a game, making a game engine is more of an exercise to me at the moment. – Ivan Ivanov Sep 28 '18 at 19:33