Hone logo
Hone
Problems

Call Site Cache in JavaScript

Performance optimization often involves understanding where code is being executed. A call site cache allows you to track the stack trace of function calls, providing valuable insights for debugging, profiling, and potentially optimizing code execution paths. This challenge asks you to implement a basic call site cache in JavaScript that records and retrieves call stacks.

Problem Description

You are tasked with building a CallSiteCache class in JavaScript. This class should provide methods to record call sites for a given function and retrieve the call stack at a later point. The cache should store the call stack as an array of function names. The class should handle multiple functions being tracked independently.

Key Requirements:

  • constructor(): Initializes the cache.
  • track(functionName): Registers a function to be tracked. functionName is a string representing the name of the function. If the function is already tracked, this method should do nothing.
  • record(functionName): Records the current call stack for the specified function. The call stack should be represented as an array of strings, where each string is the name of a function in the stack. The most recent call should be the last element in the array.
  • getCachedCallSite(functionName): Retrieves the cached call stack for the specified function. If the function is not tracked or the call stack has not been recorded, it should return null.
  • clear(functionName): Removes the call site tracking for the specified function.

Expected Behavior:

  • The record() method should capture the call stack at the point of execution.
  • The getCachedCallSite() method should return the recorded call stack or null if no stack exists.
  • Multiple functions can be tracked independently.
  • The cache should not leak memory.

Edge Cases to Consider:

  • Functions with no name (anonymous functions). These should be represented as "[anonymous]" in the call stack.
  • Functions called from the global scope.
  • Functions that call themselves recursively.
  • Calling record() on a function that hasn't been tracked.
  • Calling getCachedCallSite() on a function that hasn't been tracked.
  • Calling clear() on a function that hasn't been tracked.

Examples

Example 1:

Input:
  const cache = new CallSiteCache();
  cache.track("myFunction");
  const stack = cache.record("myFunction");

Output:
  ["myFunction", "anotherFunction", "global"]

Explanation:
  "myFunction" calls "anotherFunction" which is called from the global scope. The call stack is recorded as an array of function names.

Example 2:

Input:
  const cache = new CallSiteCache();
  cache.track("functionA");
  cache.track("functionB");
  cache.record("functionA");
  const stackA = cache.getCachedCallSite("functionA");
  const stackB = cache.getCachedCallSite("functionB");

Output:
  stackA: ["functionA", "someOtherFunction", "global"]
  stackB: null

Explanation:
  "functionA" is tracked and its call stack is recorded. "functionB" is tracked but its call stack is not recorded, so it returns null.

Example 3: (Edge Case - Anonymous Function)

Input:
  const cache = new CallSiteCache();
  cache.track("myFunction");
  myFunction(); // myFunction calls an anonymous function

Output:
  ["myFunction", "[anonymous]", "global"]

Explanation:
  The call stack includes the anonymous function, represented as "[anonymous]".

Constraints

  • The functionName parameter for track, record, getCachedCallSite, and clear must be a string.
  • The call stack array returned by getCachedCallSite should contain strings only.
  • The record method should capture the call stack efficiently without introducing significant overhead. Avoid excessive memory allocation.
  • The CallSiteCache class should be reasonably performant, with track, record, and getCachedCallSite operations taking a negligible amount of time.

Notes

  • You can use Error.captureStackTrace() to get the call stack. However, be mindful of its behavior and potential impact on performance.
  • Consider using a JavaScript object (or Map) to store the cached call sites.
  • Think about how to handle errors gracefully, such as when a function name is invalid.
  • The goal is to create a functional and reasonably efficient call site cache. Focus on correctness and clarity over extreme optimization.
  • Remember to handle anonymous functions correctly.
Loading editor...
javascript