Timings
Introduction
When a script, a test run or a page feels slow, --timings shows where the time goes. Put it before the script (or anywhere after one of kscript's own commands), and when the command ends, kscript prints how long each step of the run took, which functions and built-ins took the most time, and the slowest single calls. Nothing in the script changes, and when the flag is left out, kscript records nothing at all, so normal runs are as fast as ever.
Here is a script that works out Fibonacci numbers the slow way, by calling itself:
<!-- fib.kscript -->
<fn name="fib" params="n: int">
<if $n < 2>
<return value="$n" />
</if>
<return value="@fib($n - 1) + @fib($n - 2)" />
</fn>
<let name="words" value="'the quick brown fox'->split(' ')" />
<let name="long" value="$words->filter(fn(word) => $word->count() > 3)" />
<echo value="@fib(20)" />
<echo value="$long->join(', ')" />// fib.kscript
fn fib(n: int) {
if $n < 2 {
return $n
}
return @fib($n - 1) + @fib($n - 2)
}
$words = 'the quick brown fox'->split(' ')
$long = $words->filter(fn(word) => $word->count() > 3)
echo @fib(20)
echo $long->join(', ')Run it with --timings:
kscript --timings fib.kscriptThe script prints 6765 and quick, brown as usual. Then the timings follow, on stderr, so they never mix with what the script prints:
Timings (594.3 ms in all)
┌───────────────┬───────┬──────────┬───────┐
│ Phase │ Times │ Time │ Share │
├───────────────┼───────┼──────────┼───────┤
│ start kscript │ 1 │ 74.0 ms │ 12.4% │
│ native types │ 1 │ 21.4 ms │ 3.6% │
│ packages │ 1 │ 1.14 ms │ 0.2% │
│ read │ 1 │ 413 µs │ 0.1% │
│ parse │ 1 │ 40.5 ms │ 6.8% │
│ imports │ 1 │ 160.2 ms │ 27.0% │
│ check │ 1 │ 43.3 ms │ 7.3% │
│ run │ 1 │ 198.3 ms │ 33.4% │
└───────────────┴───────┴──────────┴───────┘
Most time of their own (own: not counting the calls they made)
┌──────────┬───────────────────┬────────┬──────────┬──────────┬─────────┬──────────┐
│ Kind │ Name │ Calls │ Total │ Own │ Average │ Longest │
├──────────┼───────────────────┼────────┼──────────┼──────────┼─────────┼──────────┤
│ function │ fib fib.kscript:2 │ 21,891 │ 103.3 ms │ 103.3 ms │ 5 µs │ 103.3 ms │
│ element │ statements │ 2 │ 53.0 ms │ 36.9 ms │ 26.5 ms │ 36.3 ms │
│ lambda │ fn fib.kscript:10 │ 4 │ 8.74 ms │ 8.57 ms │ 2.18 ms │ 8.52 ms │
│ method │ list->filter │ 1 │ 14.7 ms │ 6.01 ms │ 14.7 ms │ 14.7 ms │
│ element │ echo │ 2 │ 109.7 ms │ 5.93 ms │ 54.8 ms │ 109.1 ms │
│ method │ string->split │ 1 │ 1.38 ms │ 1.38 ms │ 1.38 ms │ 1.38 ms │
│ method │ list->join │ 1 │ 398 µs │ 398 µs │ 398 µs │ 398 µs │
│ method │ string->count │ 4 │ 167 µs │ 167 µs │ 42 µs │ 165 µs │
└──────────┴───────────────────┴────────┴──────────┴──────────┴─────────┴──────────┘fib ran 21,891 times, and its 103.3 ms is about half of the 198.3 ms the script spent running, so fib is the place to make faster, not split or filter. The numbers on your machine will differ; what matters is how they compare.
Reading the summary
Phases
Every run goes through the same steps, and the first table shows each one:
| Phase | What it is |
|---|---|
start kscript |
From starting the kscript program to its first line of work: .NET starting up and loading kscript. |
native types |
Setting up the built-in types such as email, url and money, once per run. |
packages |
Loading a package the script uses, such as KScript.Data, once per package. |
read |
Reading a script's file from disk. |
parse |
Turning a script's text into its structure. |
imports |
Bringing in what the script imports, including reading, parsing and checking each imported script. |
check |
Checking the script for mistakes before any of it runs. |
run |
Running the script. |
analyse, lint |
What kscript check does to each file: its analysis, then its lint rules. |
Times is how many times the step happened: a script that imports two others reads three files. Share is the step's part of the whole time. Because imports holds the reading, parsing and checking of the scripts it brings in, the shares can add up to more than 100%.
Calls
The second table lists what took the most time of its own, so the top row is where time was really spent. Each row is one of these kinds:
| Kind | What it counts |
|---|---|
function |
A function written in KScript, such as fn fib(n: int), with the file and line it was declared on. A type's methods and extension methods count here too. |
lambda |
A short function such as fn(word) => $word->count() > 3, with where it was written. |
method |
A built-in method, such as string->split or list->filter. |
command |
A built-in command, such as @read_file or @max. |
element |
A built-in statement, such as echo or expect. statements is a .kscript file's plain lines of assignments and calls. |
test |
One test, in kscript test. |
file |
One file, in kscript check. |
request |
One request, in kscript serve. |
The columns say:
Calls: how many times it ran.Total: the time from its start to its end, calls it made included. When a function calls itself, only the outermost call counts, so the time isn't counted twice.Own:Totalwithout the time spent in the calls it made. In the example,list->filtertook 14.7 ms in all, but 8.57 ms of that was the lambda it called, so its own time is 6.01 ms.Average:Totaldivided byCalls.Longest: the single slowest run of it.
The first time anything runs, .NET compiles it, so a first call is often much slower than the rest. When something runs many times, look at Average as well as Longest.
Slowest single calls
The last table lists the slowest single calls, tests, files or requests, longest first, with when each started (At, counted from the start of kscript). It finds the one slow call among many fast ones, such as the one request that waited on a database.
Saving the timings to a file
Give --timings a file name and the timings are saved there too. The name picks the format:
kscript --timings timings.json fib.kscript
kscript --timings timings.trace.json fib.kscript
kscript --timings timings.speedscope.json fib.kscript| File name ends in | What it holds | Open it with |
|---|---|---|
.trace.json |
Every call, one by one, on a timeline. | Perfetto (drag the file in), or chrome://tracing in Chrome. |
.speedscope.json |
Every call, one by one, as a flame graph. | speedscope (drag the file in). |
any other .json |
The summary's numbers: every phase, every name with its counts and times, and the 100 slowest calls. | Any JSON tool, or a KScript script. |
The file name may come straight after --timings, as above, or after =, as in --timings=timings.json. The word after --timings is taken as the file only when it ends in .json, because a script never does. kscript makes the file's folder when it isn't there.
The .json file reads like this, with times in milliseconds (_ms) and the same names the summary shows:
{
"version": 1,
"kscript": "0.60.0",
"command": "--timings timings.json fib.kscript",
"started": "2026-12-05T13:26:41.8635774Z",
"wall_ms": 594.3,
"phases": [
{ "name": "parse", "count": 1, "total_ms": 40.5, "self_ms": 40.5, "max_ms": 40.5 }
],
"calls": [
{ "kind": "function", "name": "fib", "where": "fib.kscript:2", "count": 21891, "total_ms": 103.3, "self_ms": 103.3, "average_ms": 0.005, "max_ms": 103.3 }
],
"slowest": [
{ "kind": "function", "name": "fib", "where": "fib.kscript:2", "thread": 2, "start_ms": 481.1, "duration_ms": 103.3 }
],
"dropped_events": 0
}Since it is JSON, a KScript script can read it. This one lists the five names that took the most time of their own:
<!-- slowest.kscript: kscript slowest.kscript timings.json -->
<import package="KScript.IO" />
<let name="timings" value="@read_json($args[0])" />
<for each="call" in="$timings.calls->take(5)">
<echo>{$call.name}: {$call.self_ms} ms in {$call.count} calls</echo>
</for>// slowest.kscript: kscript slowest.kscript timings.json
import KScript.IO
$timings = @read_json($args[0])
for call in $timings.calls->take(5) {
echo "{$call.name}: {$call.self_ms} ms in {$call.count} calls"
}The trace files keep the first million calls one by one. A run with more still counts every call in the summary and the .json file, and the summary says how many calls the trace left out.
Timing tests, checks and servers
--timings works with kscript's own commands as well as with scripts. After a command, it may go anywhere:
kscript test --all --timings tests.trace.json
kscript check --timings
kscript serve --timings serve.trace.jsonkscript testtimes each test as atest, named as the test runner names it, with its file and line. Its functions and built-ins show up as they would in a script.kscript checktimes each file as afile, with itsanalyseandlintphases.kscript servetimes each request as arequestnamed by its method and path, such asGET /about. The summary prints when the server stops with Ctrl+C. Requests can overlap, so in a.trace.jsonfile they sit on a timeline of their own, namedrequests.
Things to know
- The timings go to stderr.
kscript --timings app.kscript > out.txtsaves what the script printed and still shows the timings. - For a script,
--timingsgoes before the script's name. After it,--timingsis one of the script's own arguments ($args). - Timing every call makes the run a little slower than it would otherwise be, so compare runs that were both timed.
- A statement such as
ifor a loop is part of the function it is in: its time is that function's own time. - Use the numbers to find what to make faster in KScript itself, as well as in your scripts: a slow
parseorcheckis worth reporting with the.trace.jsonfile attached.