From: Jérôme Benoit Date: Tue, 25 Oct 2022 10:02:20 +0000 (+0200) Subject: Add empty object helper implementations benchmark X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=b64305b25c9d7a56ed1d6cc8871746adb058a899;p=benchmarks-js.git Add empty object helper implementations benchmark Signed-off-by: Jérôme Benoit --- 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",