[Performance] Improve tint and shade performance#626
[Performance] Improve tint and shade performance#626thadeucity wants to merge 3 commits intostyled-components:mainfrom
Conversation
reduce the amount of if checks for default color
reduce the amount of if checks for default color
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #626 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 89 89
Lines 860 860
Branches 323 323
=========================================
Hits 860 860
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| function shade(percentage: number | string, color: string): string { | ||
| if (color === 'transparent') return color | ||
| return mix(parseFloat(percentage), 'rgb(0, 0, 0)', color) | ||
| return mix(parseFloat(percentage), '#000000', color) |
There was a problem hiding this comment.
@thadeucity Any reason that we shouldn't send this as a shorthand #000?
There was a problem hiding this comment.
The the shorthand one will have two extra Regex validations and two extra if validations (because the first check is for the normal 6 digit hex and the second one is for the 8 digit hex.
| function tint(percentage: number | string, color: string): string { | ||
| if (color === 'transparent') return color | ||
| return mix(parseFloat(percentage), 'rgb(255, 255, 255)', color) | ||
| return mix(parseFloat(percentage), '#FFFFFF', color) |
There was a problem hiding this comment.
@thadeucity Same as with mix, should we send the shorthand #fff?
bhough
left a comment
There was a problem hiding this comment.
Hi @thadeucity, thank you for taking the time to contribute. A couple small changes, and we should be abe to merge this for inclusion in our next release.
Improve the performance of the
tintandshadefunctions.It is a tiny performance improvement and lib size reduction, but it does not degrade in any shape or form the quality of the code, so I believe it is a welcome change.
The improvement is achieved by exiting the
parseToRgbfunction in the firstIFthat checks forHEX Colors, this way not testing or matching the default color unnecessarily.