@mirei/ts-collections
    Preparing search index...

    Function percentile

    • Calculates a percentile of the numeric values produced by source.

      Type Parameters

      • TElement

        Type of elements within the source iterable.

      Parameters

      • source: Iterable<TElement>

        The source iterable to inspect.

      • percent: number

        Percentile expressed as a fraction between 0 and 1 where 0 corresponds to the minimum and 1 to the maximum.

      • Optionalselector: Selector<TElement, number>

        Optional projection that extracts the numeric value for each element. Defaults to treating the element itself as numeric.

      • Optionalstrategy: PercentileStrategy

        Strategy that determines how fractional ranks are resolved. Defaults to "linear", which interpolates between neighbouring values. Alternative strategies include "nearest", "low", "high", and "midpoint".

      Returns number

      The percentile value, or NaN when source contains no elements.

      Re-throws any error thrown while iterating source or executing selector.

      source is enumerated once and buffered so the selection algorithm can determine the requested rank without fully sorting the data. When percent is outside [0, 1], the result is clamped to the range implied by strategy.

      const upperQuartile = percentile([1, 2, 3, 4, 5], 0.75);
      console.log(upperQuartile); // 4

      const responseTimes = [
      { endpoint: '/users', duration: 120 },
      { endpoint: '/users', duration: 80 },
      { endpoint: '/users', duration: 200 }
      ];
      const p95 = percentile(responseTimes, 0.95, r => r.duration, "nearest");
      console.log(p95); // 200