Skip to content

Commit 7bd2854

Browse files
committed
Improve __toString performance
Running scenario: Short attributes, empty rendered __toString() time: 0.00077986717224121 seconds __toString2() time: 0.00049495697021484 seconds Improvement: 36.533170284317% Running scenario: Short attributes, partial rendered __toString() time: 0.00054383277893066 seconds __toString2() time: 0.00031709671020508 seconds Improvement: 41.692240245506% Running scenario: Short attributes, full rendered __toString() time: 0.00030899047851562 seconds __toString2() time: 0.00012898445129395 seconds Improvement: 58.256172839506% Running scenario: Long attributes, empty rendered __toString() time: 0.0038020610809326 seconds __toString2() time: 0.0026099681854248 seconds Improvement: 31.353859660124% Running scenario: Long attributes, partial rendered __toString() time: 0.0032980442047119 seconds __toString2() time: 0.0022611618041992 seconds Improvement: 31.439311790646% Running scenario: Long attributes, full rendered __toString() time: 0.00074219703674316 seconds __toString2() time: 0.00014185905456543 seconds Improvement: 80.886604561516%
1 parent cb7809f commit 7bd2854

File tree

1 file changed

+37
-42
lines changed

1 file changed

+37
-42
lines changed

src/TwigComponent/src/ComponentAttributes.php

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -37,48 +37,43 @@ public function __construct(private array $attributes)
3737

3838
public function __toString(): string
3939
{
40-
return array_reduce(
41-
array_filter(
42-
array_keys($this->attributes),
43-
fn (string $key) => !isset($this->rendered[$key])
44-
),
45-
function (string $carry, string $key) {
46-
if (
47-
str_contains($key, ':')
48-
&& preg_match(self::NESTED_REGEX, $key)
49-
&& !preg_match(self::ALPINE_REGEX, $key)
50-
&& !preg_match(self::VUE_REGEX, $key)
51-
) {
52-
return $carry;
53-
}
54-
55-
$value = $this->attributes[$key];
56-
57-
if ($value instanceof \Stringable) {
58-
$value = (string) $value;
59-
}
60-
61-
if (!\is_scalar($value) && null !== $value) {
62-
throw new \LogicException(\sprintf('A "%s" prop was passed when creating the component. No matching "%s" property or mount() argument was found, so we attempted to use this as an HTML attribute. But, the value is not a scalar (it\'s a "%s"). Did you mean to pass this to your component or is there a typo on its name?', $key, $key, get_debug_type($value)));
63-
}
64-
65-
if (null === $value) {
66-
trigger_deprecation('symfony/ux-twig-component', '2.8.0', 'Passing "null" as an attribute value is deprecated and will throw an exception in 3.0.');
67-
$value = true;
68-
}
69-
70-
if (true === $value && str_starts_with($key, 'aria-')) {
71-
$value = 'true';
72-
}
73-
74-
return match ($value) {
75-
true => "{$carry} {$key}",
76-
false => $carry,
77-
default => \sprintf('%s %s="%s"', $carry, $key, $value),
78-
};
79-
},
80-
''
81-
);
40+
$attributes = '';
41+
42+
foreach ($this->attributes as $key => $value) {
43+
if (isset($this->rendered[$key])) {
44+
continue;
45+
}
46+
47+
if (
48+
str_contains($key, ':')
49+
&& preg_match(self::NESTED_REGEX, $key)
50+
&& !preg_match(self::ALPINE_REGEX, $key)
51+
&& !preg_match(self::VUE_REGEX, $key)
52+
) {
53+
continue;
54+
}
55+
56+
if (null === $value) {
57+
trigger_deprecation('symfony/ux-twig-component', '2.8.0', 'Passing "null" as an attribute value is deprecated and will throw an exception in 3.0.');
58+
$value = true;
59+
}
60+
61+
if (!\is_scalar($value) && !($value instanceof \Stringable)) {
62+
throw new \LogicException(\sprintf('A "%s" prop was passed when creating the component. No matching "%s" property or mount() argument was found, so we attempted to use this as an HTML attribute. But, the value is not a scalar (it\'s a "%s"). Did you mean to pass this to your component or is there a typo on its name?', $key, $key, get_debug_type($value)));
63+
}
64+
65+
if (true === $value && str_starts_with($key, 'aria-')) {
66+
$value = 'true';
67+
}
68+
69+
$attributes .= match ($value) {
70+
true => ' '.$key,
71+
false => '',
72+
default => \sprintf(' %s="%s"', $key, $value),
73+
};
74+
}
75+
76+
return $attributes;
8277
}
8378

8479
public function __clone(): void

0 commit comments

Comments
 (0)