Skip to content

Commit

Permalink
Improve benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 28, 2019
1 parent 43e7279 commit 9235506
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
12 changes: 0 additions & 12 deletions benchmarks/average.js

This file was deleted.

4 changes: 2 additions & 2 deletions benchmarks/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ export const reportResults = function(results) {
results.forEach(reportResult)
}

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

const MICROSECS_TO_NANOSECS = 1e3
6 changes: 3 additions & 3 deletions benchmarks/results.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getArray } from './array.js'
import { measure } from './measure.js'
import { average } from './average.js'
import { getStats } from './stats.js'

export const getResults = function(tasks, { repeat }) {
const loop = getArray(repeat)
Expand All @@ -25,6 +25,6 @@ const getArgResult = function({ title, main, args, variantTitle, loop }) {

const mainA = args === undefined ? main : main.bind(null, ...args)
const durations = loop.map(() => measure(mainA))
const duration = average(durations)
return { title: titleA, args, duration, durations, count: loop.length }
const duration = getStats(durations)
return { title: titleA, args, duration, count: loop.length }
}
16 changes: 16 additions & 0 deletions benchmarks/stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const getStats = function(durations) {
const averageDuration = average(durations)
return { average: averageDuration, all: durations }
}

const average = function(array) {
return sum(array) / array.length
}

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

const add = function(memo, number) {
return memo + number
}

0 comments on commit 9235506

Please sign in to comment.