query icon indicating copy to clipboard operation
query copied to clipboard

size improvements

Open TkDodo opened this issue 3 years ago • 3 comments

context

The "big" bundle size is a constant topic when TanStack Query is compared to other libraries. I think there are a few things that can be done to improve the situation:

don't transpile optional chaining

Optional chaining is used a lot in the codebase, and it's supported in 93.44% of all browsers. Transpiling it is quite costly:

- this.retryer?.continue()
+ this.retryer == null ? void 0 : this.retryer.continue()

switch to private class fields and methods

Private class fields are supported in 92.7% of all browsers. They have a bunch of advantages:

  • they are private at runtime, not just at compile time
  • because of that, they can be minified by terser
  • as per this discussion, they would help us with circular references to private fields from within the QueryCache.

proposal

  • adapt our current supported browserslist:
- Chrome >= 73
+ Chrome >= 84
- Firefox >= 78
+ Firefox >= 90
- Edge >= 79
+ Edge >= 84
- Safari >= 12.0
+ Safari >= 15
- iOS >= 12.0
+ iOS >= 15
- opera >= 53
+ opera >= 70

Out of these changes, Safari would be the "newest" supported browser, with a release date of September 2021. This would still likely mean at least 1.5 years of browser support for Safari when we release v5.

If people want to support older browsers, they can always transpile the code themselves.

  • use private class fields and methods instead of the private TypeScript keyword.

TkDodo avatar Dec 23 '22 07:12 TkDodo

@TkDodo After quick test i found out that the minimal set of browsers we need are:

Chrome >= 91
ChromeAndroid >= 91
Firefox >= 90
Edge >= 91
Safari >= 15
iOS >= 15
opera >= 77

Otherwise babel refuses to keep optional chaining and private class fields intact.

This covers 89.8 % users globally according to browsersl.ist

DamianOsipiuk avatar Dec 28 '22 22:12 DamianOsipiuk

interesting. I've played with the targets in this babel repl and the output looks fine. You can change the targets to go down one version for any browser and you should see the output change significantly. I wonder why this would be different for our lib 🤔

TkDodo avatar Dec 29 '22 08:12 TkDodo

ah, it could be related to the bugfixes flag on the present-env. Things change if this is turned off. I think we have babel 7, but this flag is only turned on per default in babel 8.

What we could do is turn that flag on manually for us and use the list I provided above to have a bit more coverage ?

TkDodo avatar Dec 29 '22 08:12 TkDodo