Write a function to measure the performance of the given function.
Topic: Javascript
Difficulty: Medium
👑 Premium

Clarifying Questions
- What is the expected output of the function?
- Should this work with both sync and async functions?
- Do we need to return value from single run, average or percentile of multiple runs?
Explanation
This javascript utility function measures the performance of a given function by executing it multiple times and recording the execution times. It handles both synchronous and asynchronous functions and manages timeouts using Promise.race. The function then calculates a statistical output (e.g., median or 50th percentile) to report consistent performance results.
Solution Code
Browser Preview
Console Output
Scale-Ups
- Add support for async functions
- Add support for multiple runs and return average or percentile
- Add timeout handling for long-running functions
Interviewer's Expectations
- Use of performance.now() for precise time measurement
- Correct handling of both sync and async functions
- Understanding of Promise, await, Promise.race, etc.
- Timeout handling for slow or hanging functions.
- Accepts configurable options like number of runs and timeout
- Measures performance over multiple runs, not just one
- Returns statistical output (e.g., median/percentile) rather than just average
#Performance#Javascript