3 ms·
On your cloneAndSetProperty, consider if you can use a prototype instead of a clone: function chainedPrototypePlusProperty(obj, prop, value) { const newO
by chrismorgan 4y ago
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).
- bouk 4y agoI've tried this and found it to be much slower