Skip to content(if available)orjump to list(if available)

The bloat of edge-case first libraries

The bloat of edge-case first libraries

10 comments

·September 21, 2025

b_e_n_t_o_n

JavaScript had to implement these checks because you could pass a function literally anything and it was on the implementation to deal with it. Typescript has this same problem tbh.

BobbyTables2

How are the download rates of these things so high?

Are there 1000 people running 100 CI pipelines/day where downloads aren’t cached?

0x20cowboy

I agree with the spirit of this, but I’d go one more step with the example:

export function clamp(value: number, min: number, max: number): number { return }

That is just adding an extra jump and entry on the callstack where you could just have done:

Math.min(Math.max(value, min), max);

Where you need it.

(The example used was probably just for illustration though)

chii

> you could just have done ...

i disagree with this particular example - it's actually a good use case for being a utility maths function in a library.

I mean, the very `min()` and `max()` function you used in the illustration could also have been used as an example, if you use the same logic!

mewpmewp2

I'm not native English, but I've done coding for way more than 10+ years, yet I would be able to understand and read quicker Math.min(Math.max(value, min), max) compared to registering in my brain what exactly "clamp" does.

In terms of DRY and cleanliness, yes "clamp" sounds awesome. But in terms of practicality and quick understanding? Math.min and Math.max there is such a frequent pattern that brain immediately understands what is being tried to do there, as opposed to exact meaning of "clamp" to me.

It may be just me though, clamp is not a word I frequently hear in English, and I see it in code sometimes, but not frequently enough to consciously register what it does as fast in my brain. Despite seeing code for probably 8h+ a day for the past 10 years.

If it was in std lib of JS, maybe then it would be better.

Like there is some sort of balance line on how frequently something is used and whether it's REALLY worth to abstract it. If it's in stdlib, use it, if it's not use it if truly it's pretty much always used.

khaledh

This is why every language needs a good standard library, which unfortunately JavaScript never had.

01HNNWZ0MV43FF

3 of those 4 examples wouldn't exist in a statically-typed language like TypeScript

ants_everywhere

Yes this is just ad hoc runtime type checking

mewpmewp2

Alternative -> could just use something like Zod or have native stdlib Zod like type of thing to improve a lot on this type of edge case handling. Although if performance is key, then might question how much this constant parsing and validation would add on top, but in 99% cases probably wouldn't matter.