diff --git a/phpbench.json b/phpbench.json new file mode 100644 index 00000000..945f0b78 --- /dev/null +++ b/phpbench.json @@ -0,0 +1,5 @@ +{ + "$schema": "./vendor/phpbench/phpbench/phpbench.schema.json", + "runner.bootstrap": "vendor/autoload.php", + "runner.path": "tests/Benchmark" +} \ No newline at end of file diff --git a/src/Html.php b/src/Html.php index 90866126..b0b009d0 100644 --- a/src/Html.php +++ b/src/Html.php @@ -1675,13 +1675,14 @@ public static function address(string|Stringable $content = '', array $attribute public static function renderTagAttributes(array $attributes): string { if (count($attributes) > 1) { - $sorted = []; + $ordered = []; foreach (self::ATTRIBUTE_ORDER as $name) { if (isset($attributes[$name])) { - $sorted[$name] = $attributes[$name]; + $ordered[$name] = $attributes[$name]; + unset($attributes[$name]); } } - $attributes = array_merge($sorted, $attributes); + $attributes = $ordered + $attributes; } $html = ''; @@ -1925,13 +1926,16 @@ public static function removeCssStyle(array &$options, string|array $properties) */ public static function cssStyleFromArray(array $style): ?string { - $result = ''; + if (empty($style)) { + return null; + } + + $pairs = []; foreach ($style as $name => $value) { - $result .= "$name: $value; "; + $pairs[] = "$name: $value;"; } - // Return null if empty to avoid rendering the "style" attribute. - return $result === '' ? null : rtrim($result); + return implode(' ', $pairs); } /** diff --git a/tests/Benchmark/HtmlBench.php b/tests/Benchmark/HtmlBench.php new file mode 100644 index 00000000..e89efb1a --- /dev/null +++ b/tests/Benchmark/HtmlBench.php @@ -0,0 +1,85 @@ +attributes = [ + 'id' => 'test-id', + 'class' => 'test-class another-class', + 'data-test' => 'value', + 'aria-label' => 'Test Label', + 'style' => 'color: red; background: blue;', + ]; + $this->content = 'Test content with characters & symbols'; + } + + /** + * @Revs(1000) + * @Iterations(10) + */ + public function benchDivCreation(): void + { + Html::div($this->content, $this->attributes)->render(); + } + + /** + * @Revs(1000) + * @Iterations(10) + */ + public function benchFormCreation(): void + { + Html::form('/submit', 'POST', $this->attributes)->render(); + } + + /** + * @Revs(1000) + * @Iterations(10) + */ + public function benchInputCreation(): void + { + Html::input('text', 'test_field', 'test_value', $this->attributes)->render(); + } + + /** + * @Revs(1000) + * @Iterations(10) + */ + public function benchAttributeRendering(): void + { + Html::renderTagAttributes($this->attributes); + } + + /** + * @Revs(1000) + * @Iterations(10) + */ + public function benchEncoding(): void + { + Html::encode($this->content); + } + + /** + * @Revs(1000) + * @Iterations(10) + */ + public function benchAttributeEncoding(): void + { + Html::encodeAttribute($this->content); + } +}