Re:Math.min(Math.max(num, min), max)
by seanwilson on Thursday August 20, 2020 @03:10PM
(#24227381)
It's cute but if you used this in an innerloop (like a game, simulation or graphics code where 'clamp' is used often), it'll generate a ton of garbage as well as potential slowdown for no good reason.
[ Parent
| on HN ]
This should be completely obvious, but largely irrelevant to the topic at hand. The linked tweet talks about how difficult it is to remember the order of the terms when you implement clamp a certain way; I just wanted to point out that with a different solution, the order surprisingly doesn’t matter at all.
Re:Math.min(Math.max(num, min), max)
by seanwilson on Thursday August 20, 2020 @03:42PM
(#24227842)
I don't think it's obvious though - many people have never optimised for garbage collection so I thought it was worth pointing out.
I still found your approach interesting and it's rare there's a perfect approach so don't take it to heart.