DenoDDeno
Powered by
krofdrakulaK
Denoβ€’4y agoβ€’
8 replies
krofdrakula

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

i'm currently exploring the
bench
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);
});
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
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);
  }
});
Deno.bench({
  name: 'a',
  prerun: () => getSample(),
  fn(data) { // `data` === `ReturnType<typeof prerun>`
    a(data, -1);
  }
});
Deno banner
DenoJoin
Chat about Deno, a modern runtime for JavaScript and TypeScript.
20,934Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Deno bench json
BubblesBBubbles / help
4y ago
Help setting up deno deploy github action for deno 2.0
DamienDDamien / help
2y ago
using data from one deno bench in another
abiAabi / help
4y ago