@jsenv/plugin-transpilation
Version:
22 lines (20 loc) • 655 B
JavaScript
import toPropertyKey from "../toPropertyKey/toPropertyKey.js";
export default (obj, key, value) => {
key = toPropertyKey(key);
// Shortcircuit the slow defineProperty path when possible.
// We are trying to avoid issues where setters defined on the
// prototype cause side effects under the fast path of simple
// assignment. By checking for existence of the property with
// the in operator, we can optimize most of this overhead away.
if (key in obj) {
Object.defineProperty(obj, key, {
value,
enumerable: true,
configurable: true,
writable: true,
});
} else {
obj[key] = value;
}
return obj;
};