From 896570f75bba05688050ab104915cd638cc327fd Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 28 Jul 2023 16:31:10 +0200 Subject: [PATCH] add deep merge benchmark MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- deep-clone-object.mjs | 10 +++++----- deep-merge-object.mjs | 43 +++++++++++++++++++++++++++++++++++++++++++ package.json | 3 +++ pnpm-lock.yaml | 15 +++++++++++++++ 4 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 deep-merge-object.mjs diff --git a/deep-clone-object.mjs b/deep-clone-object.mjs index be98817..8206cd9 100644 --- a/deep-clone-object.mjs +++ b/deep-clone-object.mjs @@ -10,19 +10,19 @@ const object = generateRandomObject() Benchmark.suite( `Deep clone object with ${Object.keys(object).length} keys`, Benchmark.add('JSON stringify/parse', (obj = object) => { - const objClone = JSON.parse(JSON.stringify(obj)) + const objCloned = JSON.parse(JSON.stringify(obj)) }), Benchmark.add('structuredClone', (obj = object) => { - const objClone = structuredClone(obj) + const objCloned = structuredClone(obj) }), Benchmark.add('lodash cloneDeep', (obj = object) => { - const objClone = _.cloneDeep(obj) + const objCloned = _.cloneDeep(obj) }), Benchmark.add('just-clone', (obj = object) => { - const objClone = clone(obj) + const objCloned = clone(obj) }), Benchmark.add('deep-clone', (obj = object) => { - const objClone = deepClone(obj) + const objCloned = deepClone(obj) }), Benchmark.cycle(), Benchmark.complete(), diff --git a/deep-merge-object.mjs b/deep-merge-object.mjs new file mode 100644 index 0000000..036f4f0 --- /dev/null +++ b/deep-merge-object.mjs @@ -0,0 +1,43 @@ +/* eslint-disable no-unused-vars */ +import Benchmark from 'benny' +import _ from 'lodash' +import merge from 'just-merge' +import deepMerge from 'deepmerge' +import { generateRandomObject } from './benchmark-utils.mjs' + +const object = generateRandomObject() +const objectToMerge = generateRandomObject() + +Benchmark.suite( + `Deep merge two objects: object with ${ + Object.keys(object).length + } keys, object with ${Object.keys(objectToMerge).length} keys`, + Benchmark.add('lodash merge', (obj = object) => { + const objMerged = _.merge(obj, objectToMerge) + }), + Benchmark.add('just-merge', (obj = object) => { + const objMerged = merge(obj, objectToMerge) + }), + Benchmark.add('deep-clone', (obj = object) => { + const objMerged = deepMerge(obj, objectToMerge) + }), + Benchmark.cycle(), + Benchmark.complete(), + Benchmark.save({ + file: 'deep-merge-object', + format: 'json', + details: true + }), + Benchmark.save({ + file: 'deep-merge-object', + format: 'chart.html', + details: true + }), + Benchmark.save({ + file: 'deep-merge-object', + format: 'table.html', + details: true + }) +).catch(err => { + console.error(err) +}) diff --git a/package.json b/package.json index 2573d22..0ae5744 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "benchmark:busy-wait": "node busy-wait.mjs", "benchmark:empty-array": "node empty-array.mjs", "benchmark:deep-clone-object": "node deep-clone-object.mjs", + "benchmark:deep-merge-object": "node deep-merge-object.mjs", "benchmark:shallow-clone-object": "node shallow-clone-object.mjs", "benchmark:is-empty-object": "node is-empty-object.mjs", "benchmark:is-undefined": "node is-undefined.mjs", @@ -54,7 +55,9 @@ "dependencies": { "benny": "^3.7.1", "deep-clone": "^4.0.0", + "deepmerge": "^4.3.1", "just-clone": "^6.2.0", + "just-merge": "^3.2.0", "lodash": "^4.17.21", "microtime": "^3.1.1", "uuid": "^9.0.0" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e50c2b0..c7ecf4f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,9 +14,15 @@ dependencies: deep-clone: specifier: ^4.0.0 version: 4.0.0 + deepmerge: + specifier: ^4.3.1 + version: 4.3.1 just-clone: specifier: ^6.2.0 version: 6.2.0 + just-merge: + specifier: ^3.2.0 + version: 3.2.0 lodash: specifier: ^4.17.21 version: 4.17.21 @@ -518,6 +524,11 @@ packages: resolution: {integrity: sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==} dev: true + /deepmerge@4.3.1: + resolution: {integrity: sha512-3sUqbMEc77XqpdNO7FRyRog+eW3ph+GYCbj+rK+uYyRMuwsVy0rMiVtPn+QJlKFvWP/1PYpapqYn0Me2knFn+A==} + engines: {node: '>=0.10.0'} + dev: false + /define-properties@1.2.0: resolution: {integrity: sha512-xvqAVKGfT1+UAvPwKTVw/njhdQ8ZhXK4lI0bCIuCMrp2up9nPnaDftrLtmpTazqd1o+UY4zgzU+avtMbDP+ldA==} engines: {node: '>= 0.4'} @@ -1473,6 +1484,10 @@ packages: resolution: {integrity: sha512-1IynUYEc/HAwxhi3WDpIpxJbZpMCvvrrmZVqvj9EhpvbH8lls7HhdhiByjL7DkAaWlLIzpC0Xc/VPvy/UxLNjA==} dev: false + /just-merge@3.2.0: + resolution: {integrity: sha512-cNh5FWt44hx4SpQS1xZU8Tzr/fQA69pqCdjbwxmaYYIOuRfA8EIg+dn1bGmIW03ZUtR2vkMOCjWKc+jIbpauSw==} + dev: false + /kleur@4.1.5: resolution: {integrity: sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ==} engines: {node: '>=6'} -- 2.34.1