Hone logo
Hone
Problems

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 sourceObject in 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:

  1. When initialized with a sourceObject and propertyName, it should create an internal cache.
  2. Accessing the property using inlineCache.property should:
    • If the property exists in sourceObject and is cached and valid, return the cached value.
    • If the property exists in sourceObject but is not cached or the cache is invalid, retrieve the value from sourceObject, cache it, and return it.
    • If the property does not exist in sourceObject, return undefined.
  3. Modifying the sourceObject's property should invalidate the cache and update the cached value on the next access.

Edge Cases to Consider:

  • sourceObject is null or undefined.
  • propertyName is an empty string or null or undefined.
  • The property exists but is null or undefined in the sourceObject.
  • The sourceObject is modified after the InlineCache is created.
  • The sourceObject is 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 InlineCache class should be implemented using JavaScript.
  • The sourceObject can be any JavaScript object (including null or undefined).
  • The propertyName should 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 Proxy to observe changes to the sourceObject. However, for simplicity, you can also manually check the property value on each access.
  • Think about how to handle the case where the sourceObject is modified after the InlineCache is 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.
Loading editor...
javascript