switch to ESM
[benchmarks-js.git] / empty-array.mjs
diff --git a/empty-array.mjs b/empty-array.mjs
new file mode 100644 (file)
index 0000000..887e0ce
--- /dev/null
@@ -0,0 +1,35 @@
+import Benchmark from 'benny'
+import { generateRandomNumberArray } from './benchmark-utils.js'
+
+const size = 10000
+let testArray = generateRandomNumberArray(size)
+
+Benchmark.suite(
+  `Empty array with ${size} elements`,
+  Benchmark.add('length = 0', () => {
+    testArray.length = 0
+  }),
+  Benchmark.add('pop loop', () => {
+    while (testArray.length > 0) {
+      testArray.pop()
+    }
+  }),
+  Benchmark.add('splice', () => {
+    testArray.splice(0, testArray.length)
+  }),
+  Benchmark.add('shift loop', () => {
+    while (testArray.length > 0) {
+      testArray.shift()
+    }
+  }),
+  Benchmark.add('new init', () => {
+    testArray = []
+  }),
+  Benchmark.cycle(),
+  Benchmark.complete(),
+  Benchmark.save({ file: 'empty-array', format: 'json', details: true }),
+  Benchmark.save({ file: 'empty-array', format: 'chart.html', details: true }),
+  Benchmark.save({ file: 'empty-array', format: 'table.html', details: true })
+).catch(err => {
+  console.error(err)
+})