Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.x] Improve performance using match expression #54481

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

amirmohammadnajmi
Copy link

Improve performance using match expression

@antonkomarev
Copy link
Contributor

antonkomarev commented Feb 5, 2025

If you say it improves performance, there should be benchmarks. How does it improve performance?

Comment on lines +25 to +29
$this->data = match (true) {
$data instanceof Collection => $data->all(),
$data instanceof JsonSerializable => $data->jsonSerialize(),
default => $data
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could even do:

Suggested change
$this->data = match (true) {
$data instanceof Collection => $data->all(),
$data instanceof JsonSerializable => $data->jsonSerialize(),
default => $data
};
$this->data = match (get_class($data)) {
Collection::class => $data->all(),
JsonSerializable::class => $data->jsonSerialize(),
default => $data
};

@GrahamCampbell
Copy link
Member

I don't think this improves performance, if anything this probably runs slightly slower than before. Basically zero difference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants