0no-co / wonka

🎩 A tiny but capable push & pull stream library for TypeScript and Flow
MIT License
709 stars 29 forks source link

Upgrade dependencies and reduce bundle size #83

Closed kitten closed 4 years ago

kitten commented 4 years ago

This PR reduces the bundle size of Wonka by 0.15kB minzipped. It also seems to benefit the micro-benchmarks and hence seems to increase performance (measured using perf/suite.js).

Before:

    ✔  Wonka  391,189.51  ops/sec  ±0.51%  (92 runs)  -39.56%
    ✔  RxJS   382,301.23  ops/sec  ±1.13%  (92 runs)  -40.93%
    ✔  most   647,192.32  ops/sec  ±0.61%  (93 runs)  fastest

After:

    ✔  Wonka  638,620.33  ops/sec  ±1.07%  (89 runs)   -1.84%
    ✔  RxJS   382,071.19  ops/sec  ±0.79%  (91 runs)  -41.27%
    ✔  most   650,596.10  ops/sec  ±0.54%  (92 runs)  fastest

Changes

This at least passes all uses of urql

The new transpilation turns the Curry application helpers into shorter variants, e.g.:

// This guarantees that only one argument is applied, and the call is otherwise curried
Curry._1(fn, arg1);

// This is simply turned into a ternary:
var _arg1 = arg1,
  fn.length === 1 ? fn(_arg1) : fn.bind(null, _arg1)

// And if the ternary isn't actually assigned or used and is just a statement, we can just do:
var _arg1 = arg1,
  fn(_arg1)

// This is also done for the other applications with higher arity:
Curry._2(fn, arg1, arg2);
// turns into...
var _arg1 = arg1, _arg2 = arg2,
  fn.length === 2 ? fn(_arg1, _arg2) : fn.bind(null, _arg1, _arg2)

This does show the extra vars in the scope, which are created because the arguments may be expressions. But they're minified away if they're not 🤷