unadlib / mutative

Efficient immutable updates, 2-6x faster than naive handcrafted reducer, and more than 10x faster than Immer.
http://mutative.js.org/
MIT License
1.53k stars 16 forks source link

fix(copy): align behavior of shallow copy with object spread #26

Closed exuanbo closed 6 months ago

exuanbo commented 6 months ago

Follow up on https://github.com/unadlib/mutative/issues/25

In real-world use, there won't be tons of symbol keys in an object, so any slowdown in performance shouldn't be a big deal.

Also, I think we might need some more test cases for this one.


Reference: how esbuild transforms spread syntax:

var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
const state = { count: 1 };
const newState = __spreadValues({}, state);

https://hyrious.me/esbuild-repl/?version=0.19.10&t=const+state+%3D+%7B+count%3A+1+%7D%3B%0Aconst+newState+%3D+%7B+...state+%7D%3B&o=--target%3Des6

exuanbo commented 6 months ago

The CI failure comes from coveralls :thinking: