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

Use static variable and performance suggestion #10

Closed
vanthuanqs opened this issue Nov 3, 2023 · 2 comments
Closed

Use static variable and performance suggestion #10

vanthuanqs opened this issue Nov 3, 2023 · 2 comments

Comments

@vanthuanqs
Copy link

First, I want to say thank you for awesome framework.

I have been working around the framework and I see that in response() function, whenever you call it, it create a new object from Response class.
I don't think it's really needed since in one life cycle of a HTTP request, we need only one object of Response

I create a response_intance() function:

function response_instance(): Response
{
    static $responseInstance;
    if (is_null($responseInstance)) {
        $responseInstance = response();
    }

    return $responseInstance;
}

I also tested the performance:

$starttime = microtime(true);
for ($i = 0; $i < 10 * 1000 * 1000; $i++) {
    response_instance();
}
echo "response_instance() time : ", (microtime(true) - $starttime), " ms\n";

$starttime = microtime(true);
for ($i = 0; $i < 10 * 1000 * 1000; $i++) {
    response();
}
echo "response() time : ", (microtime(true) - $starttime), " ms\n";

and the result:

response_instance() time : 0.28365898132324 ms
response() time : 2.3586940765381 ms

It's much more faster than create new object every time you call the function
So I think we better create only one instance for these types of variables, example Request, Cache, ...

@amirfaramarzi
Copy link
Member

Hi

I am very happy for your cooperation

I wanted to do this request before, but I didn't get the chance. I will be very happy if you create this pull request.

@amirfaramarzi
Copy link
Member

Hi @vanthuanqs,

The announced feature is now available 🔥

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

No branches or pull requests

2 participants