Hacker News new | past | comments | ask | show | jobs | submit login

On your cloneAndSetProperty, consider if you can use a prototype instead of a clone:

  function chainedPrototypePlusProperty(obj, prop, value) {
    const newObject = Object.create(obj);
    newObject[prop] = value;
    return newObject;
    // Alternatively: return Object.create(obj, { [prop]: { configurable: true, enumerable: true, value } });
  }
It depends entirely on what you’re doing with it (not mutating obj while you keep the returned object, not depending on properties being own), but in some cases this will be suitable, and depending on the data and what you’re doing, could be drastically faster (it’s O(1) rather than O(n) on the number of properties, and access after could be much of a muchness).



I've tried this and found it to be much slower




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: