Aperture UI
Performance

Scripting performance

What script costs in an Aperture UI frame. The bridge between C++ and V8, compiled-script caching, the tier-up budget, data binding and page habits.

JavaScript is part of your frame. The goal is not to avoid scripting. It is to keep it predictable: few bridge calls, small changes to the DOM, and nothing that makes the engine redo work it already did. This page covers what costs what. Performance measurement lists the tools that measure it. Use the host-to-page function calls described in Scripting: define an update function once and call it when state changes, rather than compiling source for each field.

Where script runs

All script for a view runs on the thread that calls tick(), buildCommandList() or the view's input and script functions. That includes timers, requestAnimationFrame callbacks, event handlers and your native functions. A long callback delays everything after it in the frame, and if that thread is your game thread, it delays the game.

A host can give a view a thread of its own. The CitySlice sample does this for its React HUD, so the UI's script and layout do not stall the world, and every call into the view is posted to that thread. The rule is the same either way: treat a view as single-threaded (see Threads).

Web Workers are not implemented (see How Aperture UI differs from a browser). Do heavy computation, sorting and save parsing in the game, and send the page a compact result.

Crossing between C++ and the page

Each crossing between C++ and V8 costs more than a call inside either one. Choose the cheapest tool that fits, and cross less often.

To do thisUseCost
Push state every frame or every eventcallJavaScriptFunctionJson("fn", json)No source is compiled. V8 parses the JSON straight into a value.
Declarative UI that follows datasetDataModel and data-bind-*One script evaluates every affected binding. See Data binding.
A one-off setup or diagnosticexecuteJavaScriptCompiles the source on each call, unless an identical source is already cached.
Read a value nowevaluateJavaScriptA full round trip. Not for a per-frame loop.
Report an event to the gameA native functionThe call crosses back. Arguments arrive as strings.

The point of callJavaScriptFunctionJson is that the page defines the function once and the host sends only data. Building a script by pasting numbers into text, such as "setHealth(72)", produces a different source every time, and a source the engine has not seen before has to be compiled. Compare the two forms:

// Compiles a new script on every change.
const std::string source = "setHealth(" + std::to_string(hp) + ");";
view.executeJavaScriptRaw("hud", reinterpret_cast<const uint8_t*>(source.data()), source.size());

// Compiles nothing. One call, one object.
view.callJavaScriptFunctionJson("hud.update", "{\"health\":72,\"ammo\":18}");

Put the whole screen's state in one object and one call, and send it when it changes. See Scripting for the full set of calls.

The APUIBinder model APIs that older documentation describes (CreateModel, UpdateModel) are not part of this surface and are not a substitute for it. Use the data-model and function-call surface in the integration guide. A method that a header declares can still have an incomplete implementation, so follow the guide for what works.

Compiled scripts

The engine keeps compiled scripts, so the same source compiles once. IScriptCacheProvider (ApertureSDK::SetScriptCacheProvider) goes further and stores V8's bytecode between runs, which saves compile time at start-up for a large page. Scripts above a size threshold are compiled with V8's streaming compiler instead of the synchronous path. ApertureSDK::SetScriptStreamingThreshold sets that threshold, and you must set it before you create a view. See SDK settings.

APUI_JS_PROF=1 prints how many scripts ran, how long they took to compile and to run, how many times the compiled-script cache hit and missed, and how long garbage collection took. Many cache misses can mean that the source text changes from call to call.

Tier-up

V8 starts a function in its interpreter and compiles it to faster code after it has run for a while. Its default thresholds assume long-running hot loops. UI code is the opposite shape: an event handler or a React render runs briefly, a few times a second, and then stops, so it can stay in the interpreter and never get optimized.

JSEngineConfig::tierUpBudget makes V8 promote such functions sooner. The default, 0, keeps V8's own default. A lower budget costs a little more compilation for functions that run only once, which is why the engine leaves it opt-in. Set it before you initialize the SDK:

aperture::JSEngineConfig js;
js.tierUpBudget = 1000; // an example value, not a recommendation: measure on your own screens
aperture::ApertureSDK::ConfigureJavaScriptEngine(js);
// ... then ApertureSDK::Initialize(...)

Set APUI_JS_PROF=1 and compare the run time with and without the budget on a real screen. To try other V8 flags without a rebuild, set APUI_V8_FLAGS in the environment. Its text is appended to the flags V8 starts with, for example --always-sparkplug to compile every function with the baseline compiler. V8 reads the flags once, when it starts, and they belong to V8, not to Aperture UI, so a flag can change or disappear with a V8 upgrade. Treat it as an experiment tool and not as configuration you ship.

Data binding

Bindings run in the page and cost script time on every update:

  • An update to a model evaluates every binding that reads it in one script run. Publish a model when its data changes, not on every tick.
  • Each data-bind-for list rebuilds its markup on every update and replaces the children when the result differs. Keep bound lists short.
  • Begin each expression with the model name. Otherwise an update does not reach it. See Data binding.

Page habits

  • Keep requestAnimationFrame callbacks short. They run inside tick(). Move heavy work to the game, precompute data before you send it, or spread work over frames.
  • Batch your reads and writes. Reading a computed style or a geometry value after a change makes the engine run the pending style, and for geometry reads layout, at once. A loop that alternates them pays that on every pass. Do all the reads, then all the writes.
  • Toggle a class to change state, and animate through the paint-only inline properties. One class change restyles an element once. For something that changes every frame, set transform or opacity through the inline style, and the engine skips layout. See Rendering.
  • Cache DOM queries. Look an element up once and keep the reference while the element lives, instead of querying for it in every handler.
  • Build the DOM once. Rebuilding a large subtree each frame runs style, layout and paint for all of it. Change the state of what exists.
  • Avoid per-frame allocation in hot paths. Reuse arrays and objects instead of creating new ones every frame, and keep object shapes stable, so V8 has less to optimize and less to collect. APUI_JS_PROF shows the garbage-collection time.
  • Remove listeners, cancel timers and remove players you no longer need. Remove a screen's event listeners and cancel its timers when the screen is torn down, and remove Lottie players from the DOM, because a hidden one keeps running.

On this page