Time a Luau snippet over repeated runs (micro-benchmark)
profile-codeBenchmark a Luau snippet by running it multiple times and timing each execution with os.clock(), returning min, max, average, and total wall-clock stats for comparing code speed.
Instructions
Micro-benchmark a Luau snippet by running it runs times and timing each run with os.clock(), returning the wall-clock statistics so you can measure how fast (or how variable) a piece of code is. Use this to compare two implementations, to find out how expensive a function call / loop / property access really is, or to confirm a fix actually made something faster. The code is COMPILED ONCE via loadstring (a syntax error is returned cleanly as { error } and nothing is run); then each of the runs invocations is timed individually inside its own pcall so a runtime error in one run is counted but does not abort the benchmark. Timing measures the run only — compile time is excluded. Note os.clock() resolution is coarse, so for very cheap snippets raise runs or wrap a loop inside your code. Requires loadstring and os.clock (both guarded). Returns { runs, totalMs, avgMs, minMs, maxMs, errorCount, firstError? } or { error }. Signature: { code: string, runs: any?, threadContext: number? }. Phase: act; cost=high; idempotency=contextual-write. Requires: active-client, explicit-mutation-approval, validated-source. Produces: structured-result. Verify with: assert-state. Safety: MUTATING; writes executor workspace filesystem. On failure: inspect tool-schema for exact fields, defaults, constraints, and an invocation example.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | The Luau snippet to benchmark. Compiled once with loadstring, then executed `runs` times. It may do anything (call a function, run a loop, read properties); any value it returns is ignored — only the elapsed time per run is measured. Wrap an inner loop here if a single execution is too cheap to time accurately. | |
| runs | No | How many times to execute the compiled snippet (default 1, clamped 1..100000). More runs give a more stable average for cheap code but take longer. Each run is timed and pcall-guarded independently. | |
| threadContext | No | Optional Roblox thread identity for this call; omit it to use the server default. |