From b64305b25c9d7a56ed1d6cc8871746adb058a899 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 25 Oct 2022 12:02:20 +0200 Subject: [PATCH] Add empty object helper implementations benchmark MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- empty-object.js | 38 ++++++++++++++++++++++++++++++++++++++ package.json | 1 + 2 files changed, 39 insertions(+) create mode 100644 empty-object.js diff --git a/empty-object.js b/empty-object.js new file mode 100644 index 0000000..17be7fe --- /dev/null +++ b/empty-object.js @@ -0,0 +1,38 @@ +const Benchmark = require('benny') + +const emptyLiteral = {} + +Benchmark.suite( + 'Empty object', + Benchmark.add('Reflect keys', (obj = emptyLiteral) => { + return Reflect.ownKeys(obj).length === 0 && obj.constructor === Object + }), + Benchmark.add('Keys iteration ', (obj = emptyLiteral) => { + if (obj.constructor !== Object) return false + // Iterates over the keys of an object, if + // any exist, return false. + // eslint-disable-next-line no-unreachable-loop + for (const _ in obj) return false + return true + }), + Benchmark.add('Object keys', (obj = emptyLiteral) => { + return obj.constructor === Object && !Object.keys(obj).length + }), + Benchmark.cycle(), + Benchmark.complete(), + Benchmark.save({ + file: 'empty-object', + format: 'json', + details: true + }), + Benchmark.save({ + file: 'empty-object', + format: 'chart.html', + details: true + }), + Benchmark.save({ + file: 'empty-object', + format: 'table.html', + details: true + }) +) diff --git a/package.json b/package.json index f402a27..74f6bcd 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "prepare": "node prepare.js", "benchmark:busy-wait": "node busy-wait.js", "benchmark:empty-array": "node empty-array.js", + "benchmark:empty-object": "node empty-object.js", "benchmark:quick-select": "node quick-select.js", "benchmark:max": "node max.js", "benchmark:promise-handling": "node promise-handling.js", -- 2.34.1