Skip to content

MarcoWorms/isomorphic-benchmark

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

npm install isomorphic-benchmark --save

Node

const runBenchmark = require('isomorphic-benchmark')

Browser (adds a global "runBenchmark")

<script src="./dist/benchmark.js"></script>

Example test

const aBenchmark = {
  description: 'sum',
  iterations: 10,
  tests: [
    {
      description: 'sugar',
      amount: 100000,
      testFunc: () => {
        var a
        a += 1
      }
    },
    {
      description: 'nosugar',
      amount: 100000,
      testFunc: () => {
        var a
        a = a + 1
      }
    },
  ]
}

Example result handling in node:

const results = runBenchmark(aBenchmark)

var fs = require('fs');
var outputFilename = './' + results.description + '.json';

fs.writeFile(outputFilename, JSON.stringify(results, null, 4), function(err) {
  if(err) {
    console.log(err);
  } else {
    console.log("JSON saved to " + outputFilename);
  }
});

Example result handling in browser:

const results = runBenchmark(aBenchmark)
console.log(JSON.stringify(results, null, 4))

or displayed as tabular data using console.table

const results = runBenchmark(aBenchmark);
results.iterations.forEach(console.table)

Full example:

const aBenchmark = {
  description: 'sum',
  iterations: iterationAmount,
  tests: [
    {
      description: 'sugar',
      amount: 100000,
      testFunc: (persist) => {
        persist.test.foo += 1
        persist.iteration.bar += 1
      }
    },
    {
      description: 'nosugar',
      amount: 100000,
      testFunc: (persist) => {
        persist.test.foo = persist.test.foo + 1
        persist.iteration.bar = persist.iteration.bar + 1
      }
    },
  ],
  beforeEachTestFunc: (persist) => {
    // you can add stuff to persist other than test or iteration that will persist the whole benchmark.
    persist.myVar = 1
  },
  persistTest: () => {
    return {
      foo: 0
    }
  },
  persistIteration: () => {
    return {
      bar: 0
    }
  }
}

About

Benchmark stuff with javascript in either node or browser

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published