function timed(fn) {
return function(...args) {
const start = Date.now();
const result = fn.apply(this, args);
console.log(Date.now() - start, 'ms');
return result;
};
}A timing decorator measures how long a function takes to execute.
A decorator can measure and track function execution time.
Challenge: Implement a timed(fn) decorator that wraps a function. The wrapper should:
(The timing behavior is internal; we're testing that the result is correct)
Example:
const slow = (x) => x * 2
const timedSlow = timed(slow)
timedSlow(5) // returns 10