TinyBase logoTinyBase

MetricAggregateAdd

The MetricAggregateAdd type describes a function that can be used to optimize a custom MetricAggregate by providing a shortcut for when a single value is added to the input values.

(
  metric: Metric,
  add: number,
  length: number,
): Metric | undefined
TypeDescription
metricMetric

The current value of the Metric.

addnumber

The number being added to the Metric's aggregation.

lengthnumber

The length of the array of numbers in the Metric's aggregation.

returnsMetric | undefined

Some aggregation functions do not need to recalculate the aggregation of the whole set when one value changes. For example, when adding a new number to a series, the new sum of the series is the new value added to the previous sum.

If it is not possible to shortcut the aggregation based on just one value being added, return undefined and the Metric will be completely recalculated.

When possible, if you are providing a custom MetricAggregate, seek an implementation of an MetricAggregateAdd function that can reduce the complexity cost of growing the input data set. See the setMetricDefinition method for more examples.

Since

v1.0.0