Skip to content

satisfactory-dev/benchmark.js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Satisfactory.dev Benchmark.js Fork

Coverage Status Workflow Status

Forked from benchmark.js

Documentation

In a browser:

<script type="importmap">
{
  "imports": {
    "@satifactory-dev/benchmark": "../path/to/benchmark.js"
  }
}
</script>
<script type="module">
  import Benchmark from '@satifactory-dev/benchmark';
</script>

Using npm:

$ npm i --save @satisfactory-dev/benchmark

In Node.js:

import Benchmark from '@satisfactory-dev/benchmark';

Optionally, use the microtime module by Wade Simmons:

npm i --save microtime
import Benchmark from '@satisfactory-dev/benchmark';

function microtime() {
  const version = globalThis?.process?.version || '';
  if (
    version.startsWith('v21.') ||
    version.startsWith('v22.') ||
    version.startsWith('v23.')
  ) {
    console.warn('microtime appears to misbehave on node 21-23');

    return;
  }

  try {
    const result = require('microtime');

    console.log('using microtime');

    return result.now;
  } catch {
  }

  console.log('not using microtime');

  return undefined;
}

const maybe_microtime = microtime();

if (maybe_microtime) {
  Benchmark.Timer.changeContext({
    usTimer: maybe_microtime,
  });
}

Usage example:

var suite = new Benchmark.Suite;

// add tests
suite.add('RegExp#test', function() {
  /o/.test('Hello World!');
})
.add('String#indexOf', function() {
  'Hello World!'.indexOf('o') > -1;
})
// add listeners
.on('cycle', function(event) {
  console.log(String(event.target));
})
.on('complete', function() {
  console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({ 'async': true });

// logs:
// => RegExp#test x 4,161,532 +-0.99% (59 cycles)
// => String#indexOf x 6,139,623 +-1.00% (131 cycles)
// => Fastest is String#indexOf

Bencher Integration

logger.js

import {
  bencherLogger,
  bencherReduction,
  extractStats,
} from '@satisfactory-dev/benchmark/integrations/bencher';

function reducer(was, stats) {
  was[`${stats.suite}::${stats.benchmark}`] = {
    hz: {
      value: stats.hz,
    },
    rme: {
      value: stats.stats.rme,
    },
    sample: {
      value: stats.stats.mean,
      lower_value: stats.sample.sort((a, b) => a - b)[0],
      upper_value: stats.sample.sort((a, b) => b - a)[0],
    },
  }

  return was;
}

async function* formatter(
  cycleFormatter,
  reducer,
  suiteSets,
) {
  for (const suites of suiteSets) {
    const result = await Suite.formatCycles(cycleFormatter, ...suites);
    yield bencherReduction(reducer, ...result);
  }
}

async function logger(suiteSets) {
  return bencherLogger(
    extractStats,
    reducer,
    formatter,
    suiteSets,
    process.stdout,
  )
}

export default logger;

example_perf.js

import Benchmark from '@satifactory-dev/benchmark';
import logger from 'logger.js';

// suite definitions goes here, pretend we have `const suites = [];`

// if one exports the suites, one can execute the benchmarks in batch
export default suites;

// if one checks to see if the file is called directly, one can execute just these benchmarks
if (process.argv[1] === import.meta.filename) {
  await logger([suites]);
}

Support

Tested in Tested in Chromium (143.0.7499.4), Firefox (144.0.2), WebKit (26.0), Node (20-25)

About

A benchmarking library. As used on jsPerf.com.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 53.5%
  • JavaScript 34.5%
  • HTML 6.2%
  • CSS 4.6%
  • Makefile 1.2%