Description
The problem
It seems that Yoga API calls are relatively expensive because of WASM overhead. I've noticed that getting all computed values of a tree takes almost as long as the initial Yoga layout calculation. Correct me if I'm wrong, but I believe this is because of the barrier between JS and WASM. To get all computed values, 13 API calls must be made for each node: getComputedPadding/getComputedMargin/getComputedBorder must all be called 4 times for each edge. getComputedLayout must also be called.
My use case
Every time Yoga executes, I must get all computed values and transfer them to my custom layout system.
Benchmarks
I did a few benchmarks by setting up a tree with N nodes, and then getting the computed values of each node. Environment: Windows 10, Chrome 120.0.6099.200, Yoga 2.0.1. The benchmarks in Firefox have lower times, but the ratio between Yoga execution and getting the computed results is the same, if not worse.
100 nodes:
- Yoga execution: 1.1ms
- Getting all computed values (13 API calls): 1.3ms
- Calling getComputedLayout() only: 0.6ms
625 nodes:
- Yoga execution: 4.8ms
- Getting all computed values (13 API calls): 4.4ms
- Calling getComputedLayout() only: 2ms
2,500 nodes:
- Yoga execution: 18ms
- Getting all computed values (13 API calls): 14ms
- Calling getComputedLayout() only: 6.5ms
API proposal
I'm proposing a general getComputedValues()
method that returns an object that includes all computed values for all edges so that the computed values of a node can be retrieved with only 1 API call. To make the method more versatile, there could be parameters that specify which values and edges to return. Perhaps a 'recursive' option could be added as well so that the computed values of an entire subtree could be returned with 1 API call.