Use class instances for common objects handled in image loading optimization logic #933
Labels
[Plugin] Optimization Detective
Issues for the Optimization Detective plugin
[Plugin] Performance Lab
Issue relates to work in the Performance Lab Plugin only
[Type] Enhancement
A suggestion for improvement of an existing feature
Currently all logic is happening in functions, which in a few places makes the code complicated to follow. Specifically the handling of data arrays could be simplified, and potentially in a few places be made more efficient (e.g. by storing things in a class property rather than recalculating again). See various comments on unit test PR.
Here's an initial proposal:
ILO_URL_Metric
).url
,slug
,viewport
,elements
.elements
entry may deserve some further thought, particularly around how it could work considering additional use-cases than LCP elements in the future.ILO_Grouped_URL_Metric_Collection
).ILO_URL_Metric
instances and then take care of grouping them accordingly. The grouping internally could just be a map ofbreakpoint => ILO_URL_Metric[]
.Traversable
, so that it can be iterated just like an array (over the breakpoints).unshift( ILO_URL_Metric $metric )
that replaces theilo_unshift_url_metrics()
function and integrates the given metric properly, while discarding excess metrics beyond the configured sample size at the same time.ilo_group_url_metrics_by_breakpoint()
function could then use these two classes.ilo_get_breakpoint_max_widths()
and the sample size fromilo_get_url_metrics_breakpoint_sample_size()
. If so, we could probably avoid those function calls elsewhere and only call them inilo_group_url_metrics_by_breakpoint()
. In other words, only the group class constructor would take them as parameters (for flexibility and testing), but the wrapper function to instantiate the group class wouldn't, it would just use the default ones, to avoid having to pass those parameters around in several places.$grouped_url_metrics
array should be updated to expect an instance of the new group class. If there are particular usages where another method on that class would be helpful, it could be added based on need.$url_metrics
array but then immediately group them without doing anything else to the original array should be modified to expect an instance of the new group class as well, avoiding another call to the grouping function. This also helps separating responsibilities.ilo_parse_stored_url_metrics()
).@westonruter There may be more than the above, but those are the things I consider most essential to simplify a couple things, and they'd be the most foundational ones (basically use objects for things that are commonly passed around and aren't just simple scalar values). Please let me know your thoughts on the above proposal.
The text was updated successfully, but these errors were encountered: