Performance
Every running transition of every handler is stepped by one shared loop, which runs only while a transition is running. The first frame of a transition is sent inside setState, and later frames are sent by the loop. A transition takes its own duration however long the frames take, because the loop reads the time from the clock.
onTransitioncallbacks must not yield. A yield holds up the loop, and with it every running transition.- A callback that raises an error stops only its own connection. The error is still reported in the output, and the other connections keep running.
The numbers come from the benchmark in tests/bench/, run twice in one session in Roblox Studio on one machine. A range shows the two runs, and a single call takes microseconds, so runs differ. How the numbers changed from one version to the next is on the Performance changes page. Studio caps its frame rate, so the cost of a frame is measured with the manual clock instead of real frames.
One operation at a time
Creating, once per rule, handler, or connection:
| Operation | Time |
|---|---|
A schema such as float or union | 0.7–4 µs |
A record schema with 3 fields | 2.3–4.9 µs |
A rule with 2 fields, with or without tags and allowedTransitions | 4.6–8.1 µs |
| A rule with 20 fields | 27–38 µs |
VideState.rules.extendDynamic | 9.5–11 µs |
rule.new with 4 states of 2 fields | ≈6 µs |
rule.new with 4 states of 20 fields | 52–55 µs |
rule.new and destroy | ≈11 µs |
Connecting to a handler, where the connection is the first of its kind on that handler:
| Operation | Time |
|---|---|
onChange | 0.2–0.5 µs |
onEnter | 0.2–0.3 µs |
progress | 0.8–1.0 µs |
animate | 1.7–3.1 µs |
onTransition | 1.6–2.4 µs |
onTransition with six tweenOverrides, where the matching one is last | 12–22 µs |
A handler makes each of its features, such as its events or its transitions, the first time one of them is used, so a handler that only binds its sources and calls setState never makes them.
Easings:
| Operation | Time |
|---|---|
VideState.bezier | ≈0.8 µs |
VideState.spring with options it has not seen before | 1.2–1.3 ms |
VideState.spring with options it has seen before | 18–20 µs |
Evaluating a spring at one point | ≈0.3 µs |
Evaluating a bezier at one point | ≈0.2 µs |
Make a spring once, when the module loads, and keep it. The first call with a set of options solves for it, and a later call with the same options is answered from a cache, but it still builds the key, so it is not free.
Running:
| Operation | Time |
|---|---|
setState with nothing connected | 1.4–1.6 µs |
setState with one onChange | ≈2.5 µs |
setState with one onEnter and one onExit | 1.7–1.8 µs |
setState with one onTransition, animate, or progress connection | 8.6–10.5 µs |
setState with three animate connections | 16–21 µs |
setState with a spring easing, a delay, or instant = true | 4–11 µs |
setState with one animate, on a static rule with shared states | 5.4–7.9 µs |
pause and resume | ≈0.4 µs |
scaleSpeed and finish | 0.3–0.4 µs |
playSequence of three instant steps, started and stopped | 35–42 µs |
everyHandler.setState on 1,000 handlers, with or without stagger | 8–10 ms, about 9 µs per handler |
everyHandler.count with a filter, on 1,000 handlers | ≈0.55 ms |
setState checks the state it changes to. With nothing that receives the values, such as an onChange listener or a transition connection, it does nothing more. With one, it also reads the values of the state it leaves, so the callbacks get both, and each connection starts its tween. What the connections change most is the cost of the frames that follow.
Cost of a frame
With 1,000 handlers that are all in a transition, one step of the shared loop took 0.71–0.86 ms with one animate connection on each handler, and 2.1–2.5 ms with three. That is about 0.8 µs for each connection, for a frame. At 60 frames per second a frame has 16.7 ms, so 1,000 connections use about 5% of it. The benchmark waits for the collector before it measures the frames.
Those handlers move both of their fields. After its first frame, an animate writes only the fields that move, so a look that changes one field costs less. With 1,000 handlers of four fields, where only the transparency moves, a frame took 0.48–0.49 ms.
Many handlers at once
A transition on many handlers at once, each with one animate or onTransition connection and two tweened fields:
| Handlers | Connecting all of them | setState on all of them |
|---|---|---|
| 1,000 | 10–20 ms | 8–11 ms |
| 5,000 | 55–96 ms | 39–58 ms |
| 10,000 | 133–195 ms | 86–118 ms |
animate and onTransition cost about the same. tweenOverrides with six entries, where the matching entry was the last one, added about 15 µs to connecting each handler, and a connection with no overrides pays nothing.
Compared with hand-written code
Compare.spec.luau runs the same job twice: many components that each move a number and a UDim2 source from one state to another over the same duration with a linear easing. One side uses VideState (rule.new and animate), and the other is a hand-written shared loop. Both sides are stepped with the same delta using the manual clock, and both write the same sources.
The hand-written side is the best case for hand-written code, so the gap is the most you would see. It has no easing choice, delay, pause, speed scale, or validation, and it does not continue an interrupted transition. VideState does all of those.
| Components | Create | Start (setState) | One frame | |
|---|---|---|---|---|
| 1,000 | VideState, dynamic rule | ≈19 ms | 10.5–11 ms | ≈0.74 ms |
| VideState, static rule | 9–17 ms | 7–9 ms | ≈0.74 ms | |
| By hand | 0.3–0.6 ms | 0.1–0.2 ms | 0.17–0.19 ms | |
| 5,000 | VideState, dynamic rule | 101–116 ms | 57–61 ms | ≈9.3 ms |
| VideState, static rule | 85–101 ms | 41–44 ms | 8.7–9.9 ms | |
| By hand | 2.1–2.9 ms | 1.2–1.8 ms | 1.0–1.2 ms | |
| 10,000 | VideState, dynamic rule | 198–210 ms | ≈115 ms | ≈26 ms |
| VideState, static rule | 140–189 ms | 101–106 ms | 26–30 ms | |
| By hand | 3–11 ms | ≈1.9 ms | 2.1–2.3 ms |
The test waits for the collector to finish before each round. Without that wait, the garbage of one round was collected during the next, and whichever side ran second at 10,000 components looked several times slower.
A static rule takes plain values, checks them once in new, and never checks them again. It created handlers and started transitions faster than a dynamic rule. A frame costs the same, because a running transition does not read the states.
In this test a VideState frame costs about 4 times a hand-written one at 1,000 components, about 8 to 9 times at 5,000, and about 11 to 13 times at 10,000. Creating the components costs more, because each handler checks its states. At 1,000 components, a frame costs about 0.74 ms, which is under 5% of a 60 fps frame, and the cost grows with the number of components that are animating at the same moment, not with the number that exist. Most interfaces animate far fewer than that at once.
Compared with plain Vide sources
Many components only switch between looks, with no animation. PlainSources.spec.luau compares that case: a component with a number and a UDim2 field, either as two plain Vide sources written from a table of values when the state changes, or as a handler whose sources are bound instead. Each field is bound to a Vide effect, as a property would be, so a change also pays for the update.
| Per component | Plain Vide sources | Static rule | Static rule, shared frozen states | Dynamic rule |
|---|---|---|---|---|
| Create | 0.5–0.8 µs | 3.5–7.1 µs | 2.7–4.2 µs | 8.7–9.4 µs |
| Change the state, with the fields bound | ≈0.65 µs | 0.8–0.9 µs | 0.8–1.0 µs | ≈2.1 µs |
| Read a field | ≈0.06 µs | 0.13–0.15 µs | 0.09–0.12 µs | 0.22–0.24 µs |
- Changing the state of a handler of a static rule costs about the same as writing the sources by hand, and reading a field costs a fraction of a microsecond more.
- A dynamic rule checks the values of its sources on every change, which is the difference in its row.
- Creating a handler costs about 3 to 9 µs more than two sources, because it checks its states. 1,000 components take about 4 to 10 ms to create.
- Handlers of a static rule that share one frozen states table share one check, so each costs about 2 to 3 µs more than two sources. See Sharing states between handlers.
So a handler can stand in for a set of plain sources where the states are known, and it adds the checks, the events, and the transitions when you want them.
Choosing a rule
What each choice costs per component, from the comparison with plain Vide sources above. See Choose a static or a dynamic rule for when each fits.
| You need | Use | Create | Change the state |
|---|---|---|---|
| Looks that only switch, with no checks or transitions | Plain Vide sources | 0.5–0.8 µs | ≈0.65 µs |
| Fixed looks per state, the same for many components | Static rule with a shared frozen table | 2.7–4.2 µs | 0.8–1.0 µs |
| Fixed looks per state, different for each component | Static rule | 3.5–7.1 µs | 0.8–0.9 µs |
| Values that change while the component exists | Dynamic rule | 8.7–9.4 µs | ≈2.1 µs |
A frame of a running transition costs the same on both kinds of rule, and a static rule starts one a little faster.
When to use something else
A transition through VideState costs more than a hand-written loop, as the comparison above shows. You trade that cost for the easings, delays, pause, speed control, interrupted transitions, and checks, and for not writing or maintaining that code. If you animate thousands of values at once, or a value changes on every frame, such as a health bar that follows a number, use a plain Vide source or your own loop for those.
Memory
The memory is read from Luau's heap after the collector has freed what was waiting, with lest memory. Roblox cannot force a collection, so a reading can be off by a few percent. These repeated in both runs. They are what each thing keeps alive.
| What | Memory |
|---|---|
| A rule with 2 fields | ≈4.7 KB |
| A rule with 20 fields | ≈22.7 KB |
| A handler with 4 states of 2 fields, without the Vide sources of its states | ≈3.8 KB |
The first onChange of a handler | ≈1.1 KB |
The first onEnter of a handler | ≈1.0 KB |
The first onTransition of a handler | ≈3.6 KB |
The first progress of a handler | ≈3.9 KB |
The first animate of a handler, with two sources | ≈4.3 KB |
The first onTransition of a handler, with six tweenOverrides | ≈13.7 KB |
A running transition with one animate connection, while it runs | ≈2.9 KB |
- A rule is shared by all of its handlers, so make one rule for many components.
- The first connection of a kind includes the feature it needs, such as the events or the transitions of the handler. A later connection of the same kind costs less, about 0.2 KB for an event and 1 to 2 KB for a transition.
- 1,000 handlers with one
animateconnection each hold about 8 MB, and a transition running on all of them holds about 3 MB more. - A connection with six
tweenOverridesholds about four times what one without them does. That only matters with thousands of connections. The reading includes the tables that you pass in. - A handler that the game lets go of is collected, with or without
destroy. A handler dropped while a transition runs is collected once the transition ends.
Measure your own use. The numbers depend on the device, the number of fields, and the work your callbacks do.