Skip to content

Commit

Permalink
Improve benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 28, 2019
1 parent 8af21db commit 24a8d40
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 18 deletions.
4 changes: 2 additions & 2 deletions benchmarks/mean.js → benchmarks/average.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Calculate the mean of an array of integers
export const mean = function(array) {
// Calculate the average of an array of integers
export const average = function(array) {
return sum(array) / array.length
}

Expand Down
25 changes: 23 additions & 2 deletions benchmarks/format.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import prettyFormat, { plugins } from 'pretty-format'

export const getTitle = function(value) {
import { isPlainObject } from './utils.js'

// Retrieve unique test titles for each loop.
// Users can customize titles by using the iterated function parameters.
export const getTitles = function(values) {
return values.map(getTitle).join(' ')
}

const getTitle = function(value) {
if (hasTitle(value)) {
return value.title
}

const title = serialize(value)

const titleA = truncateTitle(title)
return titleA
}

// `{ title }` can be used to override the serialization logic
const hasTitle = function(param) {
return (
isPlainObject(param) &&
typeof param.title === 'string' &&
param.title.trim() !== ''
)
}

// We use `pretty-format` because it:
// - handles most JavaScript types
// - works in browsers
Expand Down Expand Up @@ -42,7 +63,7 @@ const truncateTitle = function(title) {
return `${start}${ELLIPSIS}${end}`
}

const MAX_TITLE_SIZE = 120
const MAX_TITLE_SIZE = 40
const ELLIPSIS = '...'
const TRUNCATE_START_LENGTH = Math.ceil((MAX_TITLE_SIZE - ELLIPSIS.length) / 2)
const TRUNCATE_END_LENGTH = Math.floor((MAX_TITLE_SIZE - ELLIPSIS.length) / 2)
20 changes: 15 additions & 5 deletions benchmarks/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@ const getArgs = function(size) {
const getArg = function(index, size) {
const dimensions = 2 ** index
const unitLength = 2 ** (2 ** (size - index - 1))
const title = `${dimensions} dimensions`
const unit = getArray(unitLength)
const argsA = getArray(dimensions).map(() => unit)
return argsA
return [{ title, args: argsA }]
}

const args = getArgs(5)
const allArgs = getArgs(5)

printResults([{ name: 'test-cartesian', func: testCartesian, args }], {
count: 1e2,
})
printResults(
[
{
name: 'test-cartesian',
func: ({ args }) => testCartesian(...args),
args: allArgs,
},
],
{
count: 1e2,
},
)
6 changes: 2 additions & 4 deletions benchmarks/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ export const printResults = function(funcs, { count }) {
results.forEach(printResult)
}

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

const MICROSECS_TO_NANOSECS = 1e3
10 changes: 5 additions & 5 deletions benchmarks/results.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getArray } from './array.js'
import { getTitle } from './format.js'
import { getTitles } from './format.js'
import { measure } from './measure.js'
import { mean } from './mean.js'
import { average } from './average.js'

export const getResults = function(funcs, { count }) {
const countA = getArray(count)
Expand All @@ -15,11 +15,11 @@ const getResult = function({ name, func, allArgs, count }) {
}

const getArgResult = function({ name, func, args, count }) {
const argsTitle = args.map(getTitle).join(' ')
const argsTitle = getTitles(args)
const title = `${name} with ${argsTitle}`

const funcA = func.bind(null, ...args)
const durations = count.map(() => measure(funcA))
const duration = mean(durations)
return { title, name, duration, durations, count }
const duration = average(durations)
return { title, duration, durations, count }
}
8 changes: 8 additions & 0 deletions benchmarks/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Is a plain object, including `Object.create(null)`
export const isPlainObject = function(value) {
return (
typeof value === 'object' &&
value !== null &&
(value.constructor === Object || value.constructor === undefined)
)
}

0 comments on commit 24a8d40

Please sign in to comment.