Optimize large form rendering#19550
Merged
Merged
Conversation
Signed-off-by: Dan Harrin <git@danharrin.com>
Member
Author
|
Test file for ref <?php
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Schema;
use Filament\Tests\Fixtures\Livewire\Livewire;
use Filament\Tests\TestCase;
use Illuminate\Support\MessageBag;
use Illuminate\Support\ViewErrorBag;
use function Filament\Tests\livewire;
uses(TestCase::class);
it('measures render performance', function (): void {
view()->share('errors', (new ViewErrorBag)->put('default', new MessageBag));
$cases = [
'TextInput' => RenderPerformanceTextInput::class,
'Select' => RenderPerformanceSelect::class,
'FileUpload' => RenderPerformanceFileUpload::class,
'Repeater (10 items, 2 fields)' => RenderPerformanceRepeater::class,
'Checkbox' => RenderPerformanceCheckbox::class,
];
$iterations = 100;
$results = [];
foreach ($cases as $label => $class) {
$test = livewire($class);
$instance = $test->instance();
$schema = $instance->getSchema('form');
$schema->toHtml();
$start = hrtime(true);
for ($i = 0; $i < $iterations; $i++) {
$schema->toHtml();
}
$totalMs = (hrtime(true) - $start) / 1_000_000;
$results[$label] = $totalMs / $iterations;
}
$width = max(array_map('strlen', array_keys($results)));
echo "\n";
echo "Render performance (avg ms per render, {$iterations} iterations):\n";
echo str_repeat('-', $width + 16) . "\n";
foreach ($results as $label => $avgMs) {
echo str_pad($label, $width + 2) . number_format($avgMs, 3) . " ms\n";
}
echo str_repeat('-', $width + 16) . "\n";
expect(true)->toBeTrue();
});
class RenderPerformanceTextInput extends Livewire
{
public function form(Schema $form): Schema
{
return $form->components([
TextInput::make('text'),
]);
}
}
class RenderPerformanceSelect extends Livewire
{
public function form(Schema $form): Schema
{
return $form->components([
Select::make('select')
->options([
'one' => 'One',
'two' => 'Two',
'three' => 'Three',
]),
]);
}
}
class RenderPerformanceFileUpload extends Livewire
{
public function form(Schema $form): Schema
{
return $form->components([
FileUpload::make('file'),
]);
}
}
class RenderPerformanceRepeater extends Livewire
{
public function form(Schema $form): Schema
{
return $form->components([
Repeater::make('items')
->schema([
TextInput::make('title'),
TextInput::make('description'),
])
->default(array_fill(0, 10, [
'title' => 'Title',
'description' => 'Description',
])),
]);
}
}
class RenderPerformanceCheckbox extends Livewire
{
public function form(Schema $form): Schema
{
return $form->components([
Checkbox::make('checkbox'),
]);
}
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.