FrontendAtlas
Interview Questions
Home>JavaScript interview questions> JavaScript coding challenges >Measure Function Duration (Profiling Wrapper)

Measure Function Duration (Profiling Wrapper)

intermediate
By FrontendAtlas Editorial · Updated Feb 3, 2026
Implement measure(fn, now?) to profile execution time. It should run a function, measure the duration using the provided clock, and return { result, durationMs }. It must support both sync and async functions (return a Promise in all cases).

Arguments

  • fn: () => any | Promise<any> — Function to execute and measure.
  • now: () => number — Optional clock (defaults to performance.now or Date.now).

Returns

Promise<{ result: any, durationMs: number }> — The function result plus elapsed time in ms.
Examples
await measure(() => doWork())

await measure(async () => await fetchData(), () => 0)

Solution

Overview

Capture a start time, execute the function, then capture an end time after it finishes. Wrap the result in Promise.resolve so both sync and async paths are handled consistently.

1

Approach: Promise.resolve wrapper

Steps:

1) Record start time.

2) Call fn and wrap with Promise.resolve.

3) After it resolves, record end time and return { result, durationMs }.

4) If fn throws or rejects, propagate the error.

Complexity: O(1) extra work (not counting fn).

export default async function measure(fn, now = () => Date.now()) {
  const start = now();
  const result = await Promise.resolve().then(fn);
  const end = now();
  return { result, durationMs: end - start };
}
export default async function measure(
  fn: () => any | Promise<any>,
  now: () => number = () => Date.now()
): Promise<{ result: any; durationMs: number }> {
  const start = now();
  const result = await Promise.resolve().then(fn);
  const end = now();
  return { result, durationMs: end - start };
}

Notes & Pitfalls

Pitfalls
  • Using Date.now() for small durations can be too coarse; prefer performance.now when available.
  • Measuring async work without awaiting gives misleading durations.
  • Profiling adds overhead; keep wrappers lightweight.
Edge cases
  • Function throws: error should propagate (no swallowing).
  • Async function resolves: duration should cover await time.
  • Clock function can be injected for deterministic tests.
Techniques
  • Promise.resolve to normalize sync vs async.

Common mistakes on this challenge

  • measure wrapper is not timing/promisifying function execution correctly

    Wrapper should support sync/async functions uniformly and rethrow errors.

    • Capture start/end timestamps around `await Promise.resolve().then(fn)`.
    • Return `{ result, durationMs }` for successful execution.
    • Allow sync throws and async rejections to propagate without swallowing.

Resources

  • MDN – Performance.now()

Similar questions

Run With a Performance Budget (Sync or Async)intermediateCreate a Spy Function (Test Double)intermediateCleanup Bag (Dispose Subscriptions)easy

Guides

Frontend interview preparation guideGuideFrontend coding interview questions and prep guideBlueprintJavaScript Problems That Actually Show UpBlueprintBuild Great UI in 60 MinutesBlueprint

Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.

Open frontend interview questionsBrowse JavaScript interview questionsOpen Essential 60Open Machine Coding HubOpen Frontend Coding ChallengesOpen System DesignOpen Interview Prep GuideOpen System Design BlueprintOpen Framework Prep PathsOpen Study PlansOpen JavaScript mastery study planOpen Company PrepOpen JavaScript Framework Prep Guide
↗Incidents hub
← Prev←60 / 88