krofdrakulaK
Denoβ€’3y agoβ€’
8 replies
krofdrakula

Setting up complex values for `Deno.bench` function runs

i'm currently exploring the bench tools in Deno and have come across a case where i can't seem to be able to implement.

in the simplest case, let's say we wanted to compare two functions that mutate a given array, but that array is nontrivially large:

const a = <T>(arr: T[], ...items:T[]): T[] => {};
const b = <T>(arr: T[], ...items:T[]): T[] => {};

const getSample = () => {
  const sample:number[] = [];
  for (let i = 0; i < 1e6; i++) sample.push(i);
  return sample;
};

Deno.bench('a', () => {
  const data = getSample();
  a(data, -1);
});

Deno.bench('b', () => {
  const data = getSample();
  b(data, -1);
});


the problem with these benches is that they build the data within the benchmark function, which i do not want to measure as part of the benchmark.

i couldn't find an analog prerun function in the docs, is there a way to pass data created outside the measured function to it? something like:

Deno.bench({
  name: 'a',
  prerun: () => getSample(),
  fn(data) { // `data` === `ReturnType<typeof prerun>`
    a(data, -1);
  }
});
Was this page helpful?