From 2addff127563e4b14defeb395b5ee031e7431707 Mon Sep 17 00:00:00 2001 From: JounQin Date: Tue, 12 Jul 2022 18:16:42 +0800 Subject: [PATCH] chore: add benchmark (#7) --- .eslintignore | 1 + .eslintrc | 6 ++++++ .github/workflows/ci.yml | 4 ++++ .github/workflows/vercel.yml | 1 + .stylelintignore | 1 + benchmark/benchmark.txt | 11 +++++++++++ benchmark/index.js | 26 ++++++++++++++++++++++++++ package.json | 3 +++ pnpm-lock.yaml | 19 +++++++++++++++++++ 9 files changed, 72 insertions(+) create mode 100644 benchmark/benchmark.txt create mode 100644 benchmark/index.js diff --git a/.eslintignore b/.eslintignore index 7d05d61..adee4a1 100644 --- a/.eslintignore +++ b/.eslintignore @@ -3,4 +3,5 @@ dist lib CHANGELOG.md /pnpm-*.yaml +!/.github !/.*.cjs diff --git a/.eslintrc b/.eslintrc index f608b51..eb3d552 100644 --- a/.eslintrc +++ b/.eslintrc @@ -7,6 +7,12 @@ "rules": { "react/react-in-jsx-scope": "off" } + }, + { + "files": ".github/*.yml", + "rules": { + "unicorn/filename-case": "off" + } } ] } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6378c74..7a1d655 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -42,5 +42,9 @@ jobs: EFF_NO_LINK_RULES: true PARSER_NO_WATCH: true + - name: Benchmark + run: node benchmark + if: matrix.node != 14 + - name: Codecov uses: codecov/codecov-action@v3 diff --git a/.github/workflows/vercel.yml b/.github/workflows/vercel.yml index 7a17eff..e46bba4 100644 --- a/.github/workflows/vercel.yml +++ b/.github/workflows/vercel.yml @@ -4,6 +4,7 @@ on: push: branches: - main + # eslint-disable-next-line yml/no-empty-mapping-value pull_request: jobs: diff --git a/.stylelintignore b/.stylelintignore index da8d662..27d3112 100644 --- a/.stylelintignore +++ b/.stylelintignore @@ -4,5 +4,6 @@ lib LICENSE *.json *.log +*.txt *.yaml *.yml diff --git a/benchmark/benchmark.txt b/benchmark/benchmark.txt new file mode 100644 index 0000000..53f6aaf --- /dev/null +++ b/benchmark/benchmark.txt @@ -0,0 +1,11 @@ +cpu: Apple M1 Max +runtime: node v16.15.1 (arm64-darwin) + +benchmark time (avg) (min … max) p75 p99 p995 +-------------------------------------------------------- ----------------------------- +yocto-ttl-cache 34.7 µs/iter (30.92 µs … 650.58 µs) 33.58 µs 84.79 µs 95.29 µs +@isaacs/ttlcache 114.71 µs/iter (99.75 µs … 3.53 ms) 108.71 µs 289.75 µs 714.38 µs + +summary + yocto-ttl-cache + 3.31x faster than @isaacs/ttlcache diff --git a/benchmark/index.js b/benchmark/index.js new file mode 100644 index 0000000..63e6f0c --- /dev/null +++ b/benchmark/index.js @@ -0,0 +1,26 @@ +// @ts-check + +import { run, bench, baseline } from 'mitata' + +baseline('yocto-ttl-cache', async () => { + const { TTLCache } = await import('../lib/index.js') + const cache = new TTLCache(1) + cache.get('key') + cache.get('key', () => 'value') + cache.set('key', 'newValue') +}) + +bench('@isaacs/ttlcache', async () => { + const { default: TTLCache } = await import('@isaacs/ttlcache') + const cache = new TTLCache({ ttl: 1 }) + cache.get('key') + cache.set('key', 'value') + // eslint-disable-next-line sonarjs/no-element-overwrite + cache.set('key', 'newValue') +}) + +await run({ + colors: !['1', 'true'].includes( + /** @type {string} */ (process.env.NO_COLOR?.toLowerCase()), + ), +}) diff --git a/package.json b/package.json index e67d771..4bcde45 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "!**/*.tsbuildinfo" ], "scripts": { + "benchmark": "NO_COLOR=1 node benchmark > benchmark/benchmark.txt", "build": "run-p build:*", "build:r": "r -f cjs", "build:ts": "tsc -p src", @@ -79,6 +80,7 @@ "@1stg/lib-config": "^8.1.1", "@changesets/changelog-github": "^0.4.5", "@changesets/cli": "^2.23.1", + "@isaacs/ttlcache": "^1.1.0", "@pkgr/webpack": "^3.1.2", "@pkgr/webpack-mdx": "^2.0.3", "@size-limit/preset-small-lib": "^7.0.8", @@ -90,6 +92,7 @@ "@types/web": "^0.0.69", "github-markdown-css": "^5.1.0", "jest-environment-jsdom": "^28.1.2", + "mitata": "^0.1.6", "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-dom": "^6.3.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7ffb5cb..0541c34 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,6 +5,7 @@ specifiers: '@1stg/lib-config': ^8.1.1 '@changesets/changelog-github': ^0.4.5 '@changesets/cli': ^2.23.1 + '@isaacs/ttlcache': ^1.1.0 '@pkgr/webpack': ^3.1.2 '@pkgr/webpack-mdx': ^2.0.3 '@size-limit/preset-small-lib': ^7.0.8 @@ -16,6 +17,7 @@ specifiers: '@types/web': ^0.0.69 github-markdown-css: ^5.1.0 jest-environment-jsdom: ^28.1.2 + mitata: ^0.1.6 react: ^18.2.0 react-dom: ^18.2.0 react-router-dom: ^6.3.0 @@ -34,6 +36,7 @@ devDependencies: '@1stg/lib-config': 8.1.1_uphbeswqf5khxvfhk4umhq6j4q '@changesets/changelog-github': 0.4.5 '@changesets/cli': 2.23.1 + '@isaacs/ttlcache': 1.1.0 '@pkgr/webpack': 3.1.2_7wjl2x7437ntxgqxgq32ggg7be '@pkgr/webpack-mdx': 2.0.3_z7ro4jost2k4sksbhba4ebu56y '@size-limit/preset-small-lib': 7.0.8_size-limit@7.0.8 @@ -45,6 +48,7 @@ devDependencies: '@types/web': 0.0.69 github-markdown-css: 5.1.0 jest-environment-jsdom: 28.1.2 + mitata: 0.1.6 react: 18.2.0 react-dom: 18.2.0_react@18.2.0 react-router-dom: 6.3.0_biqbaboplfbrettd7655fr4n2y @@ -3257,6 +3261,14 @@ packages: } dev: true + /@isaacs/ttlcache/1.1.0: + resolution: + { + integrity: sha512-XA1Ema/Bg8CcOvY637PCeYRT+i0ZIjNcOzKXlqk4TvRcNW4RhmSLBULWrVHof45qNspvUp2U4d79paLIS0qTWg==, + } + engines: { node: '>=12' } + dev: true + /@istanbuljs/load-nyc-config/1.1.0: resolution: { @@ -16175,6 +16187,13 @@ packages: } dev: true + /mitata/0.1.6: + resolution: + { + integrity: sha512-VKQ0r3jriTOU9E2Z+mwbZrUmbg4Li4QyFfi7kfHKl6reZhGzL0AYlu3wE0VPXzIwA5xnFzmEQoBwCcNT8stUkA==, + } + dev: true + /mixme/0.5.4: resolution: {