diff options
author | 2022-12-03 04:26:06 -0800 | |
---|---|---|
committer | 2022-12-03 04:26:33 -0800 | |
commit | d28a4dbb006cd3f795362d01b2b7c1fb51c17f50 (patch) | |
tree | 944e9768019dc47c4160022d65bd427ead6f9020 /bench/snippets/define-properties.mjs | |
parent | 0a30bf0212ed32a5230ec6fe9ae20fb10e52c4e4 (diff) | |
download | bun-d28a4dbb006cd3f795362d01b2b7c1fb51c17f50.tar.gz bun-d28a4dbb006cd3f795362d01b2b7c1fb51c17f50.tar.zst bun-d28a4dbb006cd3f795362d01b2b7c1fb51c17f50.zip |
Object.defineProperties snippet
Diffstat (limited to 'bench/snippets/define-properties.mjs')
-rw-r--r-- | bench/snippets/define-properties.mjs | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/bench/snippets/define-properties.mjs b/bench/snippets/define-properties.mjs new file mode 100644 index 000000000..935737b2d --- /dev/null +++ b/bench/snippets/define-properties.mjs @@ -0,0 +1,145 @@ +import { bench, run } from "../node_modules/mitata/src/cli.mjs"; + +const properties = { + closed: { + get() { + return this._writableState ? this._writableState.closed : false; + }, + }, + destroyed: { + get() { + return this._writableState ? this._writableState.destroyed : false; + }, + set(value) { + if (this._writableState) { + this._writableState.destroyed = value; + } + }, + }, + writable: { + get() { + const w = this._writableState; + return ( + !!w && + w.writable !== false && + !w.destroyed && + !w.errored && + !w.ending && + !w.ended + ); + }, + set(val) { + if (this._writableState) { + this._writableState.writable = !!val; + } + }, + }, + writableFinished: { + get() { + return this._writableState ? this._writableState.finished : false; + }, + }, + writableObjectMode: { + get() { + return this._writableState ? this._writableState.objectMode : false; + }, + }, + writableBuffer: { + get() { + return this._writableState && this._writableState.getBuffer(); + }, + }, + writableEnded: { + get() { + return this._writableState ? this._writableState.ending : false; + }, + }, + writableNeedDrain: { + get() { + const wState = this._writableState; + if (!wState) return false; + return !wState.destroyed && !wState.ending && wState.needDrain; + }, + }, + writableHighWaterMark: { + get() { + return this._writableState && this._writableState.highWaterMark; + }, + }, + writableCorked: { + get() { + return this._writableState ? this._writableState.corked : 0; + }, + }, + writableLength: { + get() { + return this._writableState && this._writableState.length; + }, + }, + errored: { + enumerable: false, + get() { + return this._writableState ? this._writableState.errored : null; + }, + }, + writableAborted: { + enumerable: false, + get: function () { + return !!( + this._writableState.writable !== false && + (this._writableState.destroyed || this._writableState.errored) && + !this._writableState.finished + ); + }, + }, +}; + +var count = 10_000; + +bench("Object.defineProperty x " + count, () => { + const prop = { + enumerable: false, + get: function () { + return !!( + this._writableState.writable !== false && + (this._writableState.destroyed || this._writableState.errored) && + !this._writableState.finished + ); + }, + }; + for (let i = 0; i < count; i++) { + function Hey() { + return this; + } + Object.defineProperty(Hey.prototype, "writableAborted", prop); + } +}); + +bench("Object.defineProperties x " + count, () => { + for (let i = 0; i < count; i++) { + function Hey() { + return this; + } + Object.defineProperties(Hey.prototype, properties); + } +}); + +bench("(all the keys) Object.defineProperties x " + count, () => { + var first; + { + function Hey() { + return this; + } + Object.defineProperties(Hey.prototype, properties); + first = Object.getOwnPropertyDescriptors(Hey.prototype); + } + + for (let i = 0; i < count; i++) { + function Hey() { + return this; + } + Object.defineProperties(Hey.prototype, first); + } +}); + +await run(); |