-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Strip undefined values from props to avoid unintentionally shadowing defaults #582
Conversation
d001167
to
9458655
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is addressing a grey zone of JavaScript semantics. If we make this change we should document it, as it not obvious to me that the API would work this way, and I would want to know that I could depend on it before starting to use it. It does mean that apps need to use null
instead of undefined
to mark empty values which is a good practice but not everyone does it.
Those doc changes should be part of the PR.
In addition, layer creation speed is important, as apps may need to create a lot of Layer
instances 60 times per second.
We actually have benchmarks that measure that (npm run bench
). So at least measure the impact of this and update the PR with the results.
New layers are instantiated and old ones disposed every frame, so this is on the critical path for performance. In that light, this PR could be refactored to avoid creating an array ( Note that this PR came from my experience with React. This SO post highlights React's handling of I suggest we sit on this for a bit to see if the risk is worth the reward -- it's a relatively small ergonomic improvement and may be mostly inconsequential. But might also be useful. Side note: this would also allow application developers to intentionally fall back to layer defaults via this pattern:
(Some of this echoes @ibgreen's comments above, which landed while I was writing this 😉 ) |
Performance difference is unclear from these tests, but here they are for posterity:
|
Thanks for collecting these. It is mainly three Interestingly, your (optimized) change seems to increase speed in 2 of 3 cases, which seems promising. (Note that there is a bit of run-to-run variability in these benchmarks due to unpredictable machine load.) It would be good to add test and benchmark cases for more layers (ideally driven by a table), and for various numbers of props (a short list of props will of course be faster to resolve than a longer list). |
Closing for now as benefits are debated and perf impact is indeterminate. |
Ran into a problem when creating a new layer in which passing props that are
undefined
shadow defaults, requiring application-layer or CompositeLayers to run checks to avoid passing a prop at all if it'sundefined
.E.g.:
In this case, the expectation is that omitting
someAccessor
will allowFooLayer
to fall back to its default definition forsomeAccessor
. Instead, the default is shadowed by theundefined
value passed as aprop
, causing a runtime error.This PR fixes that case, but I can easily imagine it will surface other bugs in which that shadowing is unintentionally expected / accounted for and unshadowing will cause other errors. So...not sure how best to proceed, but figured I'd put this out to see what you think.