<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>248754</bug_id>
          
          <creation_ts>2022-12-05 01:21:19 -0800</creation_ts>
          <short_desc>[JSC] Improve Object.defineProperties &amp; Object.defineProperty perf via fast iteration in ToPropertyDescriptor</short_desc>
          <delta_ts>2023-12-12 23:47:08 -0800</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>JavaScriptCore</component>
          <version>WebKit Nightly Build</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Jarred Sumner">jarred</reporter>
          <assigned_to name="Yusuke Suzuki">ysuzuki</assigned_to>
          <cc>mark.lam</cc>
    
    <cc>webkit-bug-importer</cc>
    
    <cc>ysuzuki</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1916645</commentid>
    <comment_count>0</comment_count>
    <who name="Jarred Sumner">jarred</who>
    <bug_when>2022-12-05 01:21:19 -0800</bug_when>
    <thetext>A diff like this in ObjectConstructor::toPropertyDescriptor improves Object.defineProperties perf 15% - 70%

```
    if (canPerformFastPropertyEnumerationForObjectAssign(description-&gt;structure())) {
        description-&gt;structure()-&gt;forEachProperty(vm, [&amp;] (const PropertyTableEntry&amp; entry) -&gt; bool {
            if (entry.attributes() &amp; PropertyAttribute::DontEnum)
                return true;

            PropertyName propertyName(entry.key());
            if (propertyName == propertyNames-&gt;enumerable) {
                desc.setEnumerable(description-&gt;getDirect(entry.offset()).toBoolean(globalObject));
                RETURN_IF_EXCEPTION(scope, false);
            } else if (propertyName == propertyNames-&gt;configurable) {
                desc.setConfigurable(description-&gt;getDirect(entry.offset()).toBoolean(globalObject));
                RETURN_IF_EXCEPTION(scope, false);
            } else if (propertyName == propertyNames-&gt;value) {
                desc.setValue(description-&gt;getDirect(entry.offset()));
                RETURN_IF_EXCEPTION(scope, false);
            } else if (propertyName == propertyNames-&gt;writable) {
                desc.setWritable(description-&gt;getDirect(entry.offset()).toBoolean(globalObject));
                RETURN_IF_EXCEPTION(scope, false);
            } else if (propertyName == propertyNames-&gt;get) {
                JSValue getter = description-&gt;getDirect(entry.offset());
                RETURN_IF_EXCEPTION(scope, false);
                if (!getter.isUndefinedOrNull() &amp;&amp; !getter.isCallable()) {
                    throwTypeError(globalObject, scope, &quot;Getter must be a function.&quot;_s);
                    return false;
                }
                desc.setGetter(getter);
            } else if (propertyName == propertyNames-&gt;set) {
                JSValue setter = description-&gt;getDirect(entry.offset());
                RETURN_IF_EXCEPTION(scope, false);
                if (!setter.isUndefinedOrNull() &amp;&amp; !setter.isCallable()) {
                    throwTypeError(globalObject, scope, &quot;Setter must be a function.&quot;_s);
                    return false;
                }
                desc.setSetter(setter);
            }

            return true;
        });

        RETURN_IF_EXCEPTION(scope, false);
    } else {
```

This diff isn&apos;t 100% correct but fast iteration through each of the leaf property descriptor objects when possible should improve perf

Here is a microbenchmark:

```
import { bench, run } from &quot;../node_modules/mitata/src/cli.mjs&quot;;

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 &amp;&amp;
        w.writable !== false &amp;&amp;
        !w.destroyed &amp;&amp;
        !w.errored &amp;&amp;
        !w.ending &amp;&amp;
        !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 &amp;&amp; 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 &amp;&amp; !wState.ending &amp;&amp; wState.needDrain;
    },
  },
  writableHighWaterMark: {
    get() {
      return this._writableState &amp;&amp; this._writableState.highWaterMark;
    },
  },
  writableCorked: {
    get() {
      return this._writableState ? this._writableState.corked : 0;
    },
  },
  writableLength: {
    get() {
      return this._writableState &amp;&amp; this._writableState.length;
    },
  },
  errored: {
    enumerable: false,
    get() {
      return this._writableState ? this._writableState.errored : null;
    },
  },
  writableAborted: {
    enumerable: false,
    get: function () {
      return !!(
        this._writableState.writable !== false &amp;&amp;
        (this._writableState.destroyed || this._writableState.errored) &amp;&amp;
        !this._writableState.finished
      );
    },
  },
};

var count = 10_000;

bench(&quot;Object.defineProperty x &quot; + count, () =&gt; {
  const prop = {
    enumerable: false,
    get: function () {
      return !!(
        this._writableState.writable !== false &amp;&amp;
        (this._writableState.destroyed || this._writableState.errored) &amp;&amp;
        !this._writableState.finished
      );
    },
  };
  for (let i = 0; i &lt; count; i++) {
    function Hey() {
      return this;
    }
    Object.defineProperty(Hey.prototype, &quot;writableAborted&quot;, prop);
  }
});

bench(&quot;Object.defineProperties x &quot; + count, () =&gt; {
  for (let i = 0; i &lt; count; i++) {
    function Hey() {
      return this;
    }
    Object.defineProperties(Hey.prototype, properties);
  }
});

bench(&quot;(all the keys) Object.defineProperties x &quot; + count, () =&gt; {
  var first;
  {
    function Hey() {
      return this;
    }
    Object.defineProperties(Hey.prototype, properties);
    first = Object.getOwnPropertyDescriptors(Hey.prototype);
  }

  for (let i = 0; i &lt; count; i++) {
    function Hey() {
      return this;
    }
    Object.defineProperties(Hey.prototype, first);
  }
});

await run();
```</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1917456</commentid>
    <comment_count>1</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2022-12-07 19:39:20 -0800</bug_when>
    <thetext>&lt;rdar://problem/103101281&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1999171</commentid>
    <comment_count>2</comment_count>
    <who name="Yusuke Suzuki">ysuzuki</who>
    <bug_when>2023-12-12 19:16:24 -0800</bug_when>
    <thetext>Pull request: https://github.com/WebKit/WebKit/pull/21717</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1999206</commentid>
    <comment_count>3</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2023-12-12 23:46:46 -0800</bug_when>
    <thetext>Committed 271972@main (2432909f3aec): &lt;https://commits.webkit.org/271972@main&gt;

Reviewed commits have been landed. Closing PR #21717 and removing active labels.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>