From: Jérôme Benoit Date: Sun, 25 Dec 2022 13:25:28 +0000 (+0100) Subject: Differentiate shallow and deep clone implementation benchmarks X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=57f622172596f25733fe81c176f8935752d2aab9;p=benchmarks-js.git Differentiate shallow and deep clone implementation benchmarks Signed-off-by: Jérôme Benoit --- diff --git a/clone-object.js b/deep-clone-object.js similarity index 67% rename from clone-object.js rename to deep-clone-object.js index 73619f2..de345fc 100644 --- a/clone-object.js +++ b/deep-clone-object.js @@ -11,23 +11,14 @@ for (let i = 0; i < size; i++) { } Benchmark.suite( - `Clone object with ${size} keys`, - Benchmark.add('structuredClone', (obj = testObject) => { - const objClone = structuredClone(obj) - }), - Benchmark.add('Spread', (obj = testObject) => { - const objClone = { ...obj } - }), - Benchmark.add('Object assign', (obj = testObject) => { - const objClone = Object.assign({}, obj) - }), + `Deep clone object with ${size} keys`, Benchmark.add('JSON stringify/parse', (obj = testObject) => { const objClone = JSON.parse(JSON.stringify(obj)) }), - Benchmark.add('lodash clone', (obj = testObject) => { - const objClone = _.clone(obj) + Benchmark.add('structuredClone', (obj = testObject) => { + const objClone = structuredClone(obj) }), - Benchmark.add('lodash deep clone', (obj = testObject) => { + Benchmark.add('lodash cloneDeep', (obj = testObject) => { const objClone = _.cloneDeep(obj) }), Benchmark.add('just-clone', (obj = testObject) => { @@ -36,17 +27,17 @@ Benchmark.suite( Benchmark.cycle(), Benchmark.complete(), Benchmark.save({ - file: 'clone-object', + file: 'deep-clone-object', format: 'json', details: true }), Benchmark.save({ - file: 'clone-object', + file: 'deep-clone-object', format: 'chart.html', details: true }), Benchmark.save({ - file: 'clone-object', + file: 'deep-clone-object', format: 'table.html', details: true }) diff --git a/package.json b/package.json index ffccc57..8eb3f31 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "prepare": "node prepare.js", "benchmark:busy-wait": "node busy-wait.js", "benchmark:empty-array": "node empty-array.js", - "benchmark:clone-object": "node clone-object.js", + "benchmark:deep-clone-object": "node deep-clone-object.js", + "benchmark:shallow-clone-object": "node shallow-clone-object.js", "benchmark:is-empty-object": "node is-empty-object.js", "benchmark:is-undefined": "node is-undefined.js", "benchmark:quick-select": "node quick-select.js", diff --git a/promise-handling.js b/promise-handling.js index b07b33d..0d0b367 100644 --- a/promise-handling.js +++ b/promise-handling.js @@ -19,9 +19,22 @@ async function asyncFunction () { Benchmark.suite( 'Promise handling', Benchmark.add('await promise', async () => { - await asyncFunction() + try { + return await asyncFunction() + } catch (e) { + console.error(e) + } }), - Benchmark.add('promise', () => { + Benchmark.add('promise with then().catch()', () => { + asyncFunction() + .then(r => { + return r + }) + .catch(e => { + console.error(e) + }) + }), + Benchmark.add('mishandled promise', () => { asyncFunction() }), Benchmark.cycle(), diff --git a/shallow-clone-object.js b/shallow-clone-object.js new file mode 100644 index 0000000..131b5e9 --- /dev/null +++ b/shallow-clone-object.js @@ -0,0 +1,40 @@ +/* eslint-disable no-unused-vars */ +const Benchmark = require('benny') +const { generateRandomInteger } = require('./benchmark-utils') +const _ = require('lodash') + +const size = generateRandomInteger(500) +const testObject = {} +for (let i = 0; i < size; i++) { + testObject[i.toString()] = i +} + +Benchmark.suite( + `Shallow clone object with ${size} keys`, + Benchmark.add('Spread', (obj = testObject) => { + const objClone = { ...obj } + }), + Benchmark.add('Object assign', (obj = testObject) => { + const objClone = Object.assign({}, obj) + }), + Benchmark.add('lodash clone', (obj = testObject) => { + const objClone = _.clone(obj) + }), + Benchmark.cycle(), + Benchmark.complete(), + Benchmark.save({ + file: 'shallow-clone-object', + format: 'json', + details: true + }), + Benchmark.save({ + file: 'shallow-clone-object', + format: 'chart.html', + details: true + }), + Benchmark.save({ + file: 'shallow-clone-object', + format: 'table.html', + details: true + }) +)