Polymorphic Inline Cache in JavaScript
This challenge asks you to implement a polymorphic inline cache in JavaScript. Inline caches are a performance optimization technique used in JavaScript engines to speed up property access on objects. Your goal is to create a generic cache that can store and retrieve values based on a key, and automatically update the cache when the underlying value changes.
Problem Description
You need to build a PolymorphicInlineCache class that acts as a cache for property values on objects. The cache should be "polymorphic" meaning it can handle different types of objects and properties. The core functionality is to store a value associated with a specific property name on an object. When the property's value changes, the cache should be updated to reflect the new value. The cache should also be able to retrieve the cached value efficiently.
Key Requirements:
- Initialization: The cache should be initialized with an object and a property name.
- Caching: When a value is initially accessed through the cache, it should retrieve the value from the object.
- Update on Change: If the property's value on the object changes, the cache should be updated to reflect the new value. This update should happen automatically.
- Retrieval: The cache should provide a method to retrieve the cached value.
- Polymorphism: The cache should work with any object and any property name (string).
- No External Dependencies: The solution should not rely on any external libraries.
Expected Behavior:
The cache should behave as a transparent layer on top of the object's property. Accessing the cache should be equivalent to accessing the property directly, but with the added benefit of caching. Changes to the property should be immediately reflected in the cache.
Edge Cases to Consider:
- Object Deletion: What happens if the object is deleted? The cache should not throw an error and should return
undefinedwhen accessed. - Property Deletion: What happens if the property is deleted from the object? The cache should return
undefinedwhen accessed. - Non-Object Input: Handle cases where the initial object is not an object.
- Property Name is Empty String: Handle cases where the property name is an empty string.
- Property Value is Null/Undefined: The cache should correctly handle null and undefined property values.
Examples
Example 1:
Input:
const obj = { x: 10 };
const cache = new PolymorphicInlineCache(obj, 'x');
Output:
cache.get() // 10
obj.x = 20;
cache.get() // 20
Explanation: The cache initially retrieves the value of obj.x. When obj.x is updated to 20, the cache automatically updates to reflect the new value.
Example 2:
Input:
const obj = { y: 'hello' };
const cache = new PolymorphicInlineCache(obj, 'y');
Output:
cache.get() // "hello"
obj.y = 'world';
cache.get() // "world"
delete obj.y;
cache.get() // undefined
Explanation: The cache works with different data types. Deleting the property results in the cache returning undefined.
Example 3: (Edge Case)
Input:
const obj = { z: null };
const cache = new PolymorphicInlineCache(obj, 'z');
Output:
cache.get() // null
obj.z = undefined;
cache.get() // undefined
Explanation: The cache correctly handles null and undefined values.
Constraints
- The object passed to the constructor must be an object or
undefined. If it'sundefined, the cache should immediately returnundefinedon anyget()call. - The property name must be a string.
- The cache should update automatically within a reasonable timeframe (no need for strict real-time synchronization, but avoid excessive delays).
- The
get()method should return the cached value orundefinedif the property is deleted or the object is deleted.
Notes
- Consider using
Proxyto intercept property access and updates. However, avoid usingProxyif it's not supported in the target environment (older browsers). A simpler approach usingObject.definePropertyand a getter/setter is acceptable. - Think about how to efficiently detect changes to the property value.
- Focus on creating a clean, readable, and maintainable solution.
- The goal is to demonstrate understanding of caching principles and object manipulation in JavaScript.