diff --git a/.eslintrc.js b/.eslintrc.js index 4fe9bd5..4f0a77b 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -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' } }, { diff --git a/package.json b/package.json index 7e57f50..962e525 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/perf/.eslintrc.js b/perf/.eslintrc.js new file mode 100644 index 0000000..5f2e52e --- /dev/null +++ b/perf/.eslintrc.js @@ -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' + } +} diff --git a/perf/index.html b/perf/index.html deleted file mode 100644 index 7bf335a..0000000 --- a/perf/index.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - updeep perf - - - - -
Running tests...
-
- -

Done!

- - diff --git a/perf/index.js b/perf/index.js index 5a45426..c88ea50 100644 --- a/perf/index.js +++ b/perf/index.js @@ -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(`

${suiteName}

') - }) - .run({ async: true }) - } + return () => new Promise((resolve, reject) => { + suite.on('complete', () => resolve({ + suiteName, + results, + })).on('error', reject).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) diff --git a/yarn.lock b/yarn.lock index f712c36..be0682b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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==