Hone logo
Hone
Problems

Method Decorator Implementation in TypeScript

Method decorators are a powerful feature in TypeScript that allow you to modify or enhance the behavior of class methods. This challenge will guide you through implementing your own method decorators, enabling you to add functionality like logging, timing, or validation to your methods in a clean and reusable way. Understanding and utilizing method decorators is crucial for writing maintainable and extensible TypeScript code.

Problem Description

You are tasked with creating a TypeScript method decorator factory called logExecutionTime. This factory should accept a prefix string as an argument and return a method decorator. The decorator, when applied to a method, should:

  1. Wrap the original method with a function that measures the execution time.
  2. Log a message to the console before and after the method's execution, including the method name, prefix, and execution time in milliseconds.
  3. Ensure the original method is still executed and its return value is returned.

The decorator factory should allow you to customize the log prefix for different methods.

Examples

Example 1:

// Code to be executed (outside the challenge)
class MyClass {
  @logExecutionTime("Method 1:")
  myMethod() {
    console.log("Executing myMethod...");
    return "Result 1";
  }

  @logExecutionTime("Method 2:")
  anotherMethod(arg: number) {
    console.log(`Executing anotherMethod with ${arg}...`);
    return arg * 2;
  }
}

const instance = new MyClass();
instance.myMethod();
console.log(instance.anotherMethod(5));

Output:

Method 1: Starting execution of myMethod...
Executing myMethod...
Method 1: myMethod execution completed in 0.0123ms
Method 2: Starting execution of anotherMethod(5)...
Executing anotherMethod with 5...
Method 2: anotherMethod execution completed in 0.0087ms
10

Example 2: (Edge Case - Method with no arguments)

// Code to be executed (outside the challenge)
class SimpleClass {
  @logExecutionTime("Simple:")
  simpleMethod() {
    return "Simple Result";
  }
}

const simpleInstance = new SimpleClass();
console.log(simpleInstance.simpleMethod());

Output:

Simple: Starting execution of simpleMethod...
Simple: simpleMethod execution completed in 0.0054ms
Simple Result

Constraints

  • The decorator factory must accept a single string argument (the prefix).
  • The decorator must correctly measure and log the execution time in milliseconds.
  • The original method must be executed, and its return value must be returned by the decorated method.
  • The logging should be clear and informative, including the prefix, method name, and execution time.
  • The code should be well-structured and easy to understand.

Notes

  • Consider using console.time() and console.timeEnd() for measuring execution time.
  • The decorator factory should return a function that accepts the method to be decorated as an argument.
  • The decorator should handle methods with and without arguments correctly.
  • Focus on the core functionality of logging execution time; error handling or more complex logging features are not required for this challenge.
  • Remember that decorators are functions that modify other functions or classes. The factory pattern allows you to create decorators with configurable parameters.
Loading editor...
typescript