switch to ESM
[benchmarks-js.git] / shallow-clone-object.mjs
diff --git a/shallow-clone-object.mjs b/shallow-clone-object.mjs
new file mode 100644 (file)
index 0000000..05a97ec
--- /dev/null
@@ -0,0 +1,38 @@
+/* eslint-disable no-unused-vars */
+import Benchmark from 'benny'
+import _ from 'lodash'
+import { generateRandomObject } from './benchmark-utils.js'
+
+const object = generateRandomObject()
+
+Benchmark.suite(
+  `Shallow clone object with ${Object.keys(object).length} keys`,
+  Benchmark.add('Spread', (obj = object) => {
+    const objClone = { ...obj }
+  }),
+  Benchmark.add('Object assign', (obj = object) => {
+    const objClone = Object.assign({}, obj)
+  }),
+  Benchmark.add('lodash clone', (obj = object) => {
+    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
+  })
+).catch(err => {
+  console.error(err)
+})