Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(useFela): optimize passing of theme & props #674

Merged
merged 2 commits into from Feb 20, 2019

Conversation

TxHawks
Copy link
Collaborator

@TxHawks TxHawks commented Feb 19, 2019

Description

This pull requests makes the following suggestions

Optimizations

In many simple use cases, the useFela hook will be used without any props passed to it, and will only make the theme available to the rule callbacks in the returned css function. In light of that,
this commit makes two optimizations:

  1. Only merge props into the object that is passed to css's callbacks when props are actually passed.
  2. Merge props into the propsWithTheme object using mutative Object.assign, as it is by far the most performant way of doing so. See: https://jsperf.com/object-assign-vs-spreading/3

These may seem like micro optimizations, but since useFela is a hot
path that could potentially run thousands of times, I think this is
of value in this case.

DX

The useFela hook now throws an error with a helpful message when it is used in a component that isn't a descendant of a RendererProvider

Packages

react-fela

Versioning

Patch

Checklist

Quality Assurance

You can also run yarn run check to run all 4 commands at once.

  • The code was formatted using Prettier (yarn run format)
  • The code has no linting errors (yarn run lint)
  • All tests are passing (yarn run test)
  • There are no flow-type errors (yarn run flow)

Changes

If one of the following checks doesn't make sense due to the type of PR, just check them.

  • Tests have been added/updated
  • Documentation has been added/updated
  • My changes have proper flow-types

@TxHawks
Copy link
Collaborator Author

TxHawks commented Feb 19, 2019

Not sure why tests are failing in the CI env, they all pass locally

In many simple use cases, the `useFela` hook will be used without any
props passed to it, and will only make the `theme` available to the
rule callbacks in the returned `css` function. In light of that,
this commit makes two optimizations:

  1. Only merge props into the object that is passed to `css`'s
     callbacks when props are actually passed.
  2. Merge props into the `propsWithTheme` object using mutative
     `Object.assign`, as it is by far the most performant way of doing
     so. See: https://jsperf.com/object-assign-vs-spreading/3

These may seem like micro optimizations, but since useFela is a hot
path that could potentially run thousands of times, I think this is
of value in this case.
Throw an error with a helpful message when the `useFela` hook is used
in a component that isn't a descendant of a RendererProvider
@TxHawks TxHawks changed the title Perf/use fela props perf(useFela): optimize passing of theme & props Feb 19, 2019
@TxHawks
Copy link
Collaborator Author

TxHawks commented Feb 19, 2019

Sorry, my bad. Fixed now

Copy link
Owner

@robinweser robinweser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Urgh, my eyes :D
I hate looking at non-functional, mutable code like that, but well fair enough. Looks like a create optimisation here.

@robinweser robinweser merged commit 664db27 into master Feb 20, 2019
@TxHawks TxHawks deleted the perf/useFela-props branch February 20, 2019 09:40
@TxHawks
Copy link
Collaborator Author

TxHawks commented Feb 20, 2019

I hate mutating like that too, but it's an object we create and completely control, so the perf gains in a hot-path seem worth it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants