Skip to content

Instantly share code, notes, and snippets.

@davidnguyen11
Last active August 16, 2019 13:02
Show Gist options
  • Save davidnguyen11/c6dab8fd64e484657195f07aa44fb5ad to your computer and use it in GitHub Desktop.
Save davidnguyen11/c6dab8fd64e484657195f07aa44fb5ad to your computer and use it in GitHub Desktop.
const chalk = require('chalk');
const log = console.log;
function runBenchmark(description, operations, callback) {
const start = process.hrtime.bigint();
for (let i = 0; i < operations; i++) {
callback();
}
const end = process.hrtime.bigint();
const nanoSeconds = end - start;
const seconds = parseFloat(nano) / 1000000000;
log(chalk.green(description));
log(`Benchmark with ${operations} operations, took ${nanoSeconds} nanoseconds - ${seconds} seconds`);
log();
}
module.exports = {
runBenchmark
};

benchmark.js

const { runBenchmark } = require('./run-benchmark');

function test() {
  console.log('hello world');
}

runBenchmark('with 1.000.000 operations', 1000000, function() {
  test();
});

package.json

{
  "scripts": {
    "benchmark": "node ./benchmark.js"
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment