alignSeries

Benchmark created on


Setup

const asc = (a, b) => a - b;

export const alignSeries = (series) => {
  const timestampsSet = new Set([...series.flatMap((item) => item.map((v) => v[0] / 1000))]);
  const timestamps = Array.from(timestampsSet).sort(asc);

  const seriesMaps = series.map((s) => new Map(s));
  const alignedSeries = series.map(() => new Array(timestamps.length).fill(null));

  // the scaleFactors array stores a scaling factor for each series
  // to ensure that extremely large values (> Number.MAX_SAFE_INTEGER)
  // are scaled down to avoid potential issues with js number precision limits
  const scaleFactors = [];

  for (let j = 0; j < series.length; j++) {
    let maxSeriesValue = Number.NEGATIVE_INFINITY;

    for (let i = 0; i < timestamps.length; i++) {
      const ts = timestamps[i];
      const v = seriesMaps[j].get(ts * 1000);

      alignedSeries[j][i] = v ?? null;

      maxSeriesValue = v && maxSeriesValue < v ? v : maxSeriesValue;
    }

    scaleFactors.push(maxSeriesValue > Number.MAX_SAFE_INTEGER ? 1e12 : 1);
  }

  return {
    scaleFactors,
    series: alignedSeries,
    timestamps,
  };
};

Test runner

Ready to run.

Testing in
TestOps/sec
normal
const input = [[[1609459200000, 10]], [[1609459200000, 20]]];

alignSeries(input);
ready
more
const input = [[[1609459200000, 10]], [[1609459200000, 20]]];

alignSeries(input);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.