Implementing an Inline Cache in JavaScript
Inline caching is an optimization technique used in JavaScript engines to speed up property access. It allows the engine to predict that a property access will likely return the same value, and stores that value locally, avoiding a full lookup in the object's prototype chain. This challenge asks you to implement a simplified version of an inline cache to understand the core concepts.
Problem Description
You are tasked with creating a JavaScript object that simulates an inline cache for a specific property. This object should manage a cached value for a given property name. When a property is accessed, the object first checks if the property exists and if it's cached. If cached and valid, it returns the cached value. If not cached or invalid, it retrieves the value from a provided sourceObject, caches it, and then returns it. The cache should be invalidated if the source object's property value changes.
Key Requirements:
- Caching: Store the value of a property from a
sourceObjectin a cache. - Retrieval: Return the cached value if available and valid.
- Invalidation: Clear the cache when the
sourceObject's property value changes. - Source Object Updates: The cache must reflect changes in the
sourceObject. - Property Existence: Handle cases where the property doesn't exist in the
sourceObject.
Expected Behavior:
The InlineCache object should behave as follows:
- When initialized with a
sourceObjectandpropertyName, it should create an internal cache. - Accessing the property using
inlineCache.propertyshould:- If the property exists in
sourceObjectand is cached and valid, return the cached value. - If the property exists in
sourceObjectbut is not cached or the cache is invalid, retrieve the value fromsourceObject, cache it, and return it. - If the property does not exist in
sourceObject, returnundefined.
- If the property exists in
- Modifying the
sourceObject's property should invalidate the cache and update the cached value on the next access.
Edge Cases to Consider:
sourceObjectisnullorundefined.propertyNameis an empty string ornullorundefined.- The property exists but is
nullorundefinedin thesourceObject. - The
sourceObjectis modified after theInlineCacheis created. - The
sourceObjectis deleted. (While not strictly required, consider how your implementation might handle this).
Examples
Example 1:
const source = { x: 10 };
const cache = new InlineCache(source, 'x');
console.log(cache.x); // Output: 10 (cache created)
source.x = 20;
console.log(cache.x); // Output: 20 (cache invalidated and updated)
Explanation: The first access creates the cache and returns 10. Modifying source.x invalidates the cache, and the second access retrieves the new value 20.
Example 2:
const source = { y: null };
const cache = new InlineCache(source, 'y');
console.log(cache.y); // Output: null (cache created with null value)
source.y = 5;
console.log(cache.y); // Output: 5 (cache invalidated and updated)
Explanation: The first access creates the cache and returns null. Modifying source.y invalidates the cache, and the second access retrieves the new value 5.
Example 3:
const source = {};
const cache = new InlineCache(source, 'z');
console.log(cache.z); // Output: undefined (property doesn't exist)
source.z = 100;
console.log(cache.z); // Output: 100 (property created and cached)
Explanation: The property 'z' doesn't exist initially, so undefined is returned. Adding 'z' to source creates the property and caches it, so the second access returns 100.
Constraints
- The
InlineCacheclass should be implemented using JavaScript. - The
sourceObjectcan be any JavaScript object (includingnullorundefined). - The
propertyNameshould be a string. - The solution should be reasonably efficient. While performance is not the primary focus, avoid unnecessary operations.
- The cache should be invalidated immediately upon changes to the source object's property.
Notes
- Consider using
Proxyto observe changes to thesourceObject. However, for simplicity, you can also manually check the property value on each access. - Think about how to handle the case where the
sourceObjectis modified after theInlineCacheis created. You'll need a mechanism to detect these changes. - This is a simplified model of an inline cache. Real-world implementations are significantly more complex and involve sophisticated heuristics. The goal here is to understand the basic principles.
- Focus on correctness and clarity over extreme optimization.