Differentiate shallow and deep clone implementation benchmarks
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 25 Dec 2022 13:25:28 +0000 (14:25 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 25 Dec 2022 13:25:28 +0000 (14:25 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
deep-clone-object.js [moved from clone-object.js with 67% similarity]
package.json
promise-handling.js
shallow-clone-object.js [new file with mode: 0644]

similarity index 67%
rename from clone-object.js
rename to deep-clone-object.js
index 73619f246e7fa90e1689a7eae6aa366ed44e8f2b..de345fcafd594693740809f1335ea530518c72bf 100644 (file)
@@ -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
   })
index ffccc57d5762bd042d2d3337d4e43f954bcc4823..8eb3f31946c97d3a3460fb058f91e08efcac4a93 100644 (file)
@@ -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",
index b07b33d835583ae15f6edf8fd26b6cbc437953eb..0d0b3671b171749b9792c7254d11887d1387469c 100644 (file)
@@ -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 (file)
index 0000000..131b5e9
--- /dev/null
@@ -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
+  })
+)