zloirock / core-js

Standard Library
MIT License
24.61k stars 1.66k forks source link

Fix create-property.js #1324

Closed stonechoe closed 10 months ago

stonechoe commented 10 months ago

This PR is for https://github.com/zloirock/core-js/issues/1322

The changes are as follows

module.exports = function (object, key, value) {
  var propertyKey = toPropertyKey(key);
  if (propertyKey in object) definePropertyModule.f(object, propertyKey, createPropertyDescriptor(0, value));
  else object[propertyKey] = value;
};
module.exports = function (object, key, value) {
  var propertyKey = toPropertyKey(key);
  definePropertyModule.f(object, propertyKey, createPropertyDescriptor(0, value));
};

I am not certain about the impact of this patch on performance - in case the existing code was intentionally written for performance benefits like JIT optimization, please handle it appropriately at your discretion.

zloirock commented 10 months ago

This PR should be to master branch.

You are right, it's a performance optimization. Without this optimization, some methods worked unacceptably slow. However, now most methods where it's used are available in most engines, and Proxy is enough popular. I will think make it sense to accept it or not.

stonechoe commented 10 months ago

Sorry for the confusion. I changed the base branch to master.

stonechoe commented 10 months ago

Thank you for your hard work for maintaining this project. May I ask a question?

Does the "unacceptable slowness" come from the fact that Object.defineProperty(object, key, createPropertyDescriptor(0, value)) cannot be optimized by engine, whereas object[key] = value can? Or does it only occur when polyfill version is used for definePropertyModule.f?

zloirock commented 10 months ago

Does the "unacceptable slowness" come from the fact that Object.defineProperty(object, key, createPropertyDescriptor(0, value)) cannot be optimized by engine, whereas object[key] = value can?

Yes. Some years ago it was dozens of times slower even in modern engines. I remember issues about optimizing this case in bug trackers of some engines. I didn't check the performance recently, so I don't know how it works in actual versions.