From ecc627943cd9d41e99fa6568bd4925877d09bf60 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 May 2021 11:32:23 +0200 Subject: [PATCH] add optics-ts tests --- package-lock.json | 8 ++++++- package.json | 1 + perf/119.ts | 59 +++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index 92dbcab..c68e6fb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "monocle-ts", - "version": "2.3.9", + "version": "2.3.10", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -5067,6 +5067,12 @@ "wrappy": "1" } }, + "optics-ts": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/optics-ts/-/optics-ts-2.1.0.tgz", + "integrity": "sha512-SJIwf16fwyo9+O+x303BifrWZ/H6Du/hS1zz4UERYt9BaRvxBID9XCYZlQ3c2aIrqeG64AxLcRy8VC/OYUkS9A==", + "dev": true + }, "optionator": { "version": "0.8.3", "resolved": "https://registry.npmjs.org/optionator/-/optionator-0.8.3.tgz", diff --git a/package.json b/package.json index b5684cf..febb9b2 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "import-path-rewrite": "github:gcanti/import-path-rewrite", "jest": "^24.3.0", "mocha": "^5.2.0", + "optics-ts": "^2.1.0", "partial.lenses": "^14.17.0", "prettier": "^2.0.2", "rimraf": "^2.6.3", diff --git a/perf/119.ts b/perf/119.ts index 6cb559b..cf67465 100644 --- a/perf/119.ts +++ b/perf/119.ts @@ -5,14 +5,38 @@ import * as T from '../src/Traversal' import { Traversable } from 'fp-ts/ReadonlyArray' import { pipe } from 'fp-ts/function' const PL = require('partial.lenses') +import * as O from 'optics-ts' /* -prism into array (monocle-ts, stable APIs) x 1,140 ops/sec ±0.49% (87 runs sampled) -prism into array (monocle-ts, experimental APIs) x 958 ops/sec ±3.06% (86 runs sampled) -prism into array (partial.lenses) x 460,357 ops/sec ±0.46% (89 runs sampled) -prism modify array (monocle-ts, stable APIs) x 1,320 ops/sec ±0.64% (86 runs sampled) -prism modify array (monocle-ts, experimental APIs) x 1,423 ops/sec ±3.40% (84 runs sampled) -prism modify array (partial.lenses) x 2,329 ops/sec ±0.49% (87 runs sampled) +size = 5000 +prism into array (monocle-ts, stable APIs) x 1,123 ops/sec ±0.37% (87 runs sampled) +prism into array (monocle-ts, experimental APIs) x 951 ops/sec ±2.18% (86 runs sampled) +prism into array (partial.lenses) x 451,070 ops/sec ±1.17% (89 runs sampled) +prism into array (optics-ts) x 10,672 ops/sec ±2.82% (84 runs sampled) +prism modify array (monocle-ts, stable APIs) x 1,336 ops/sec ±0.61% (89 runs sampled) +prism modify array (monocle-ts, experimental APIs) x 1,251 ops/sec ±2.06% (87 runs sampled) +prism modify array (partial.lenses) x 2,191 ops/sec ±2.12% (81 runs sampled) +prism modify array (optics-ts) x 3,763 ops/sec ±0.51% (92 runs sampled) + +size = 500 +prism into array (monocle-ts, stable APIs) x 10,328 ops/sec ±7.12% (87 runs sampled) +prism into array (monocle-ts, experimental APIs) x 9,230 ops/sec ±0.41% (91 runs sampled) +prism into array (partial.lenses) x 466,256 ops/sec ±0.49% (86 runs sampled) +prism into array (optics-ts) x 105,339 ops/sec ±0.45% (94 runs sampled) +prism modify array (monocle-ts, stable APIs) x 13,290 ops/sec ±1.53% (82 runs sampled) +prism modify array (monocle-ts, experimental APIs) x 12,911 ops/sec ±0.64% (88 runs sampled) +prism modify array (partial.lenses) x 30,039 ops/sec ±0.45% (91 runs sampled) +prism modify array (optics-ts) x 37,777 ops/sec ±0.46% (90 runs sampled) + +size = 50 +prism into array (monocle-ts, stable APIs) x 95,180 ops/sec ±10.31% (89 runs sampled) +prism into array (monocle-ts, experimental APIs) x 87,631 ops/sec ±0.39% (90 runs sampled) +prism into array (partial.lenses) x 462,813 ops/sec ±0.27% (90 runs sampled) +prism into array (optics-ts) x 558,323 ops/sec ±0.46% (91 runs sampled) +prism modify array (monocle-ts, stable APIs) x 111,160 ops/sec ±0.63% (88 runs sampled) +prism modify array (monocle-ts, experimental APIs) x 113,941 ops/sec ±0.59% (86 runs sampled) +prism modify array (partial.lenses) x 200,423 ops/sec ±0.56% (87 runs sampled) +prism modify array (optics-ts) x 251,464 ops/sec ±0.37% (90 runs sampled) */ const suite = new Benchmark.Suite() @@ -74,13 +98,22 @@ const traversalPartialLenses = PL.compose( const foldPartialLenses = PL.compose(traversalPartialLenses, PL.valueOr(undefined)) -const nameModified = 'Luke-' + mid + '-modified' - const traversalMonocleExperimentalModify = pipe( traversalMonocleExperimental, T.modify((s) => ({ ...s, name: nameModified })) ) +const traversalOpticsTs = O.optic() + .prop('m') + .prop('n') + .prop('names') + .elems() + // `item` here has type: `NotAnArrayType`, not sure what it means + .when((item: any) => item.id === id) + +const nameModified = 'Luke-' + mid + '-modified' +const f = (c: Child): Child => ({ ...c, name: nameModified }) + suite .add('prism into array (monocle-ts, stable APIs)', function () { foldMonocleStable.headOption(data) @@ -91,14 +124,20 @@ suite .add('prism into array (partial.lenses)', function () { PL.get(foldPartialLenses, data) }) + .add('prism into array (optics-ts)', function () { + O.preview(traversalOpticsTs)(data) + }) .add('prism modify array (monocle-ts, stable APIs)', function () { - traversalMonocleStable.modify((s) => ({ ...s, name: nameModified }))(data) + traversalMonocleStable.modify(f)(data) }) .add('prism modify array (monocle-ts, experimental APIs)', function () { traversalMonocleExperimentalModify(data) }) .add('prism modify array (partial.lenses)', function () { - PL.modify(traversalPartialLenses, (s: Child) => ({ ...s, name: nameModified }), data) + PL.modify(traversalPartialLenses, f, data) + }) + .add('prism modify array (optics-ts)', function () { + O.modify(traversalOpticsTs)(f as any)(data) }) .on('cycle', function (event: any) { // tslint:disable-next-line: no-console