📎 Webclip
A simple explanation of functional pipe in JavaScript
The article argues that pipe helps balance two pressures in JavaScript: keeping APIs broad for common types and keeping shipped code small. It shows that prototype augmentation makes methods hard to tree-shake and can create global mutation problems, while functional composition avoids both.
Reading notes#
- JavaScript apps are shipped over a network, so smaller bundles matter because download and parse time affect responsiveness.
- Tree-shaking can remove unused code only when the bundler can statically prove it is unused.
- Attaching methods to a type’s prototype gives that type a broader API, but it mutates global objects and can be overwritten by other code.
- Prototype methods are not good candidates for tree-shaking because bundlers cannot know whether third-party code depends on them.
- Rewriting prototype methods as standalone functions makes them easier to remove when unused.
- The downside of standalone function calls is that they read from right to left when nested.
- A
pipefunction restores left-to-right readability by passing a value through a sequence of functions. pipeWithaccepts the initial value first, then the functions to apply.- Higher-order functions let piped functions accept extra arguments, as in
multiplyBy(2). pipecan be used to build reusable composed functions such astripleTheOdds.- The pattern is similar to RxJS’s
pipeand the article connects it to the pipeline operator proposal in JavaScript.
