Identify likely bottlenecks in supplied code, propose behaviour-preserving optimizations, and write a benchmark plan to measure them instead of assuming gains.
Analyze the code below for performance problems. Rank causes by the work they add for the stated workload, propose behaviour-preserving changes, and provide a benchmark and profiling plan. Do not report benchmark numbers you did not measure. ## Inputs Code: ``` [CODE] ``` Environment (browser, Node.js, Python, database) and versions: [ENVIRONMENT] Symptom (slow endpoint, high memory, janky UI) and how it was observed: [SYMPTOM] Workload size (input sizes, request rate, data volume): [WORKLOAD] Profiler output, traces or timings, if available: [MEASUREMENTS] ## Instructions 1. If [MEASUREMENTS] is supplied, start from what it shows. Otherwise label findings as hypotheses. 2. Check for: - algorithmic cost (nested loops, repeated scans, quadratic string building); - memory growth or retention (unbounded caches, listeners or timers not cleaned up, large copies); - unnecessary re-renders and expensive render work (React only); - N+1 database or network calls; - repeated work that could be cached or computed once. 3. For each change, explain why it reduces work for [WORKLOAD] and confirm the output is unchanged. 4. Write a benchmark harness with representative input sizes, warm-up, several runs and the statistic to compare, using a standard tool for [ENVIRONMENT]. 5. Say which findings need a profiler to confirm, and which profiler to use. ## Return 1. Ranked findings: location, cause, expected effect on work done, confidence. 2. Optimized code. 3. Equivalence check showing the optimized version returns the same results. 4. Benchmark harness and how to run it. 5. Profiling steps for unconfirmed findings. ## Acceptance checks - No speedup percentage or timing is reported unless it comes from [MEASUREMENTS]. - Every optimization preserves output for the equivalence inputs. - React-specific advice appears only for React code.
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Code (Node.js 22):
function uniqueEmails(rows) {
const seen = [];
for (const r of rows) {
const e = r.email.trim().toLowerCase();
if (!seen.includes(e)) seen.push(e);
}
return seen;
}
Symptom: nightly import step takes much longer as the customer list grows
Workload: about 200,000 rows per run
Measurements: noneA ranked finding that seen.includes inside the loop makes the function O(n^2) in the number of unique emails; an optimized version using a Set while keeping first-seen order in the returned array; an equivalence check on rows with duplicate, mixed-case and whitespace-padded emails; a benchmark harness that times both versions at 1,000, 10,000 and 100,000 rows with warm-up runs; and no reported speedup figure because no measurements were supplied. Illustrative expectation; the equivalence of the two versions was executed locally on a synthetic fixture (see report).
Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts:
Author-supplied tool and model tags; confirm behavior with your own examples.
Models