-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
99 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |