Problem
Creating clones of immutable objects with minor alterations can be a tedious process. Write a class ImmutableHelper that serves as a tool to help with this requirement. The constructor accepts an immutable object obj which will be a JSON object or array.
The class has a single method produce which accepts a function mutator.
The function returns a new object which is similar to the original except it has those mutations applied.
mutator accepts a proxied version of obj. A user of this function can (appear to) mutate this object, but the original object obj should not actually be effected.
For example, a user could write code like this:
const originalObj = {"x": 5}; const helper = new ImmutableHelper(originalObj); const newObj = helper.produce((proxy) => { proxy.x = proxy.x + 1; }); console.log(originalObj); // {"x": 5} console.log(newObj); // {"x": 6}
Properties of the mutator function:
- It will always return
undefined. - It will never access keys that don’t exist.
- It will never delete keys (
delete obj.key) - It will never call methods on a proxied object (
push,shift, etc). - It will never set keys to objects (
proxy.x = {})
Note on how the solution will be tested: the solution validator will only analyze differences between what was returned and the original obj. Doing a full comparison would be too computationally expensive. Also, any mutations to the original object will result in a wrong answer.
Examples
Example 1:
| |
Example 2:
| |
Example 3:
| |
Constraints:
2 <= JSON.stringify(obj).length <= 4 * 10^5mutatorsis an array of functionstotal calls to produce() < 10^5
Solution
Method 1 – Proxy-based Shallow Copy
Intuition
We want to allow users to “mutate” an object or array, but without changing the original. By using a Proxy, we can intercept mutations and apply them to a shallow copy, returning the new object while keeping the original unchanged.
Approach
- In the
producemethod, create a shallow copy of the original object (array or object). - Use a Proxy to intercept set operations and apply them to the shallow copy.
- Pass the Proxy to the mutator function.
- Return the shallow copy after mutation.
- The original object remains unchanged.
Code
| |
| |
Complexity
- ⏰ Time complexity:
O(n)— Shallow copy of the object/array is made, where n is the number of keys/elements. - 🧺 Space complexity:
O(n)— Space for the shallow copy of the object/array.