Hone logo
Hone
Problems

Adaptive Compilation in JavaScript: Optimizing Function Execution

Adaptive compilation aims to optimize JavaScript function execution by dynamically selecting the most efficient implementation based on runtime conditions. This challenge asks you to build a simplified version of this concept, where you'll create a system that chooses between two different implementations of a function based on the input type. This is useful for scenarios where different input types benefit from different algorithms or optimizations.

Problem Description

You are tasked with creating a function called adaptiveFunction that takes an input value and a configuration object as arguments. The configuration object contains two function implementations: implementationA and implementationB. adaptiveFunction should analyze the type of the input value and call the appropriate implementation.

  • Input Value: Can be a number or a string.

  • Configuration Object: Must contain the following properties:

    • implementationA: A function that accepts a single argument and returns a value.
    • implementationB: A function that accepts a single argument and returns a value.
    • typeThreshold: (Optional) A number. If the input is a number and is greater than or equal to this threshold, implementationB should be used. If not provided, default to 100.
  • Behavior:

    • If the input is a number, and it's less than typeThreshold, call implementationA with the input.
    • If the input is a number, and it's greater than or equal to typeThreshold, call implementationB with the input.
    • If the input is a string, call implementationB with the input.
    • If the input is of any other type, throw a TypeError with the message "Invalid input type".

Examples

Example 1:

Input: adaptiveFunction(50, { implementationA: (x) => x * 2, implementationB: (x) => x + 10, typeThreshold: 60 })
Output: 100
Explanation: The input is a number (50), which is less than the typeThreshold (60). Therefore, implementationA(50) is called, which returns 50 * 2 = 100.

Example 2:

Input: adaptiveFunction(75, { implementationA: (x) => x * 2, implementationB: (x) => x + 10, typeThreshold: 60 })
Output: 85
Explanation: The input is a number (75), which is greater than or equal to the typeThreshold (60). Therefore, implementationB(75) is called, which returns 75 + 10 = 85.

Example 3:

Input: adaptiveFunction("hello", { implementationA: (x) => x * 2, implementationB: (x) => x.toUpperCase() })
Output: "HELLO"
Explanation: The input is a string. Therefore, implementationB("hello") is called, which returns "HELLO".

Example 4:

Input: adaptiveFunction(true, { implementationA: (x) => x * 2, implementationB: (x) => x + 10 })
Output: TypeError: Invalid input type
Explanation: The input is a boolean, which is not a number or a string. Therefore, a TypeError is thrown.

Constraints

  • The implementationA and implementationB functions in the configuration object must accept a single argument and return a value.
  • The typeThreshold must be a number. If not provided, it defaults to 100.
  • The input value can be of any type, but only numbers and strings are supported.
  • The function must handle invalid input types gracefully by throwing a TypeError.
  • The function should be reasonably performant; avoid unnecessary computations.

Notes

  • Consider using typeof to determine the type of the input value.
  • The typeThreshold provides a simple mechanism for adaptive behavior. Think about how you can use this to switch between implementations.
  • Error handling is crucial. Ensure your function throws the correct error when the input type is invalid.
  • The default value for typeThreshold should be handled correctly if it's not provided in the configuration object.
Loading editor...
javascript