Skip to content

Commit

Permalink
Add benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 27, 2019
1 parent ed4ca30 commit f64e119
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 11 deletions.
7 changes: 7 additions & 0 deletions benchmarks/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const getArray = function(length) {
return Array.from({ length }, getIndex)
}

const getIndex = function(value, index) {
return index
}
22 changes: 11 additions & 11 deletions benchmarks/main.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import testCartesian from '../src/main.js'

const getIndex = function(value, index) {
return index
}

const getArray = function(length) {
return new Array({ length }, getIndex)
}

const DATA = [[getArray(5)], [getArray(10), getArray(10)]]

console.log(testCartesian(DATA[1]))
import { printResults } from './print.js'
import { getArray } from './array.js'

printResults(
[
{ variant: 'simple', args: [getArray(5)] },
{ variant: 'complex', args: [getArray(100), getArray(100)] },
],
[{ name: 'test-cartesian', func: testCartesian }],
{ count: 1e4 },
)
12 changes: 12 additions & 0 deletions benchmarks/mean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Calculate the mean of an array of integers
export const mean = function(array) {
return sum(array) / array.length
}

const sum = function(array) {
return array.reduce(add, 0)
}

const add = function(memo, number) {
return memo + number
}
13 changes: 13 additions & 0 deletions benchmarks/measure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { performance } from 'perf_hooks'

import { promiseThen } from './promise.js'

// Measure how long a sync or async function take to execute
export const measure = function(func) {
const start = performance.now()
return promiseThen(func(), () => {
const end = performance.now()
const duration = end - start
return duration
})
}
15 changes: 15 additions & 0 deletions benchmarks/print.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getResults } from './results.js'

export const printResults = function(variants, funcs, { count }) {
const results = getResults(variants, funcs, { count })
results.forEach(printResult)
}

const printResult = function({ variant, name, duration }) {
// eslint-disable-next-line no-console, no-restricted-globals
console.log(
`${variant} ${name}: ${Math.round(duration * MICROSECS_TO_NANOSECS)}ns`,
)
}

const MICROSECS_TO_NANOSECS = 1e3
18 changes: 18 additions & 0 deletions benchmarks/promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Like `promise.then()` except if `value` is not a promise
export const promiseThen = function(value, func) {
if (isPromise(value)) {
// eslint-disable-next-line promise/prefer-await-to-then
return value.then(func)
}

return func()
}

const isPromise = function(value) {
return (
typeof value === 'object' &&
value !== null &&
// eslint-disable-next-line promise/prefer-await-to-then
typeof value.then === 'function'
)
}
23 changes: 23 additions & 0 deletions benchmarks/results.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getArray } from './array.js'
import { measure } from './measure.js'
import { mean } from './mean.js'

export const getResults = function(variants, funcs, { count }) {
const countA = getArray(count)
return variants.flatMap(({ variant, args }) =>
getVariantResult({ variant, args, funcs, count: countA }),
)
}

const getVariantResult = function({ variant, args, funcs, count }) {
return funcs.map(({ name, func }) =>
getResult({ variant, args, name, func, count }),
)
}

const getResult = function({ variant, args, name, func, count }) {
const funcA = func.bind(null, ...args)
const durations = count.map(() => measure(funcA))
const duration = mean(durations)
return { variant, name, duration, durations, count }
}

0 comments on commit f64e119

Please sign in to comment.