Performance benchmarks are ran in node

This commit is contained in:
Shaun Dern 2020-04-03 16:27:36 +01:00
parent 73b2caa838
commit c63801184f
No known key found for this signature in database
GPG Key ID: 19E354FC9294F450
6 changed files with 75 additions and 54 deletions

View File

@ -41,16 +41,7 @@ const config = {
sourceType: 'module',
},
rules: {
"no-unused-expressions": 0
}
},
{
files: ['perf/**/*'],
env: {
browser: true
},
rules: {
'no-unused-vars': 'off',
"no-unused-expressions": 'off'
}
},
{

View File

@ -27,6 +27,7 @@
"url": "https://github.com/substantial/updeep/issues"
},
"scripts": {
"benchmark": "NODE_ENV=production node --require \"@babel/register\" perf",
"build": "microbundle",
"dev": "microbundle watch",
"dtslint": "dtslint --localTs node_modules/typescript/lib types",
@ -51,6 +52,7 @@
"@typescript-eslint/parser": "^2.26.0",
"benchmark": "^2.1.4",
"chai": "^4.2.0",
"chalk": "^4.0.0",
"dtslint": "^3.4.1",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.1",
@ -62,6 +64,7 @@
"microbundle": "^0.11.0",
"mocha": "^7.0.0",
"prettier": "^2.0.2",
"table": "^5.4.6",
"typescript": "^3.6.3"
}
}

14
perf/.eslintrc.js Normal file
View File

@ -0,0 +1,14 @@
module.exports = {
extends: require.resolve("../.eslintrc.js"),
env: {
node: true,
es6: true,
},
parserOptions: {
sourceType: 'module',
},
rules: {
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'off'
}
}

View File

@ -1,14 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>updeep perf</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<div>Running tests...</div>
<div id="perf"></div>
<script src="/index.js"></script>
<p>Done!</p>
</body>
</html>

View File

@ -1,8 +1,10 @@
const Benchmark = require('benchmark')
import Benchmark from 'benchmark'
import {table} from 'table'
import chalk from 'chalk'
const _ = require('lodash')
const u = require('../lib')
const { curry2, curry4 } = require('../lib/util/curry')
import _ from 'lodash'
import u from '../dist/index.umd.js'
import {curry2, curry4 } from '../lib/util/curry'
const add4 = (a, b, c, d) => a + b + c + d
const add2 = (a, b) => a + b
@ -16,33 +18,31 @@ const updeepCurryAdd4 = curry4(add4)
const array = [0, 1, 2, 3, 4, 5]
// const doUpdate = u(x => x + 1);
function log(str) {
if (typeof document !== 'undefined') {
console.log(str)
const el = document.getElementById('perf')
el.innerHTML += str
}
}
const log = console.log
function createSuite(suiteName, tests) {
const suite = Benchmark.Suite() // eslint-disable
const results = []
return () => {
log(`<h2>${suiteName}</h2><ul>`)
const suite = Benchmark.Suite({
onCycle: (event) => {
results.push(event.target)
},
onError: (event) => {
console.error(event)
},
})
_.each(tests, (fn, testName) => {
suite.add(testName, fn)
})
suite
.on('cycle', (event) => {
log(`<li>${String(event.target)}</li>`)
return () => new Promise((resolve, reject) => {
suite.on('complete', () => resolve({
suiteName,
results,
})).on('error', reject).run({ async: true})
})
.on('complete', () => {
log('</ul>')
})
.run({ async: true })
}
}
const curryVsLodash = createSuite('Curry', {
@ -80,6 +80,25 @@ const applyVsDestructure = createSuite('apply vs destructure', {
destructure: () => fnDestructure(1, 2, 3, 4, 5),
})
curryVsLodash()
mapVsLodash()
// applyVsDestructure();
const printSuiteResults = (suiteResults) => {
const HEADERS = ['Suite Name', 'Results (fastest first)'].map(s => chalk.bold(s))
const data = suiteResults.reduce((acc, {suiteName, results}) => {
const row = [
chalk.cyan(suiteName),
results.sort((a, b) => -a.compare(b)).map(String).join('\n'),
]
acc.push(row)
return acc
}, [HEADERS])
log(table(data))
}
Promise.all([
curryVsLodash(),
mapVsLodash(),
// applyVsDestructure(),
]).then(printSuiteResults)

View File

@ -1392,6 +1392,14 @@ chalk@^3.0.0:
ansi-styles "^4.1.0"
supports-color "^7.1.0"
chalk@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.0.0.tgz#6e98081ed2d17faab615eb52ac66ec1fe6209e72"
integrity sha512-N9oWFcegS0sFr9oh1oz2d7Npos6vNoWW9HvtCg5N1KRFpUhaAhvTv5Y58g880fZaEYSNm3qDz8SU1UrGvp+n7A==
dependencies:
ansi-styles "^4.1.0"
supports-color "^7.1.0"
chardet@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e"
@ -6033,7 +6041,7 @@ symbol-observable@^1.1.0:
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.2.0.tgz#c22688aed4eab3cdc2dfeacbb561660560a00804"
integrity sha512-e900nM8RRtGhlV36KGEU9k65K3mPb1WV70OdjfxlG2EAuM1noi/E/BaW/uMhL7bPEssK8QV57vN3esixjUvcXQ==
table@^5.2.3:
table@^5.2.3, table@^5.4.6:
version "5.4.6"
resolved "https://registry.yarnpkg.com/table/-/table-5.4.6.tgz#1292d19500ce3f86053b05f0e8e7e4a3bb21079e"
integrity sha512-wmEc8m4fjnob4gt5riFRtTu/6+4rSe12TpAELNSqHMfF3IqnA+CH37USM6/YR3qRZv7e56kAEAtd6nKZaxe0Ug==