Skip to content

Reduce unnecessary calls #7

Closed
Closed
@biplobice

Description

@biplobice

public function view()
{
$this->set('corp', $this->getPackageConfig()->get('security.cross_origin_resource_policy'));
$this->set('coop', $this->getPackageConfig()->get('security.cross_origin_opener_policy'));
$this->set('coep', $this->getPackageConfig()->get('security.cross_origin_embedder_policy'));
$this->set('accessControlAllowOrigin', $this->getPackageConfig()->get('security.access_control_allow_origin'));
$this->set('nosniff', $this->getPackageConfig()->get('security.x_content_type_options'));
}
protected function getPackageConfig()
{
/** @var PackageService $packageService */
$packageService = $this->app->make(PackageService::class);
$package = $packageService->getClass('md_security_header_extended');
return $package->getFileConfig();
}

I noticed that in the code snippet above, the PackageService class is instantiated multiple times by $this->getPackageConfig() method calls, which can impact performance.

To optimize this, we can implement two approaches:

Approach 1

Retrieve the config array once and use its values directly:

$security = $this->getPackageConfig()->get('security');

Approach 2

Introduce a new property to cache the package config and reuse it:

if (!$this->pkgConfig) {
    $this->pkgConfig = // Set the property here
}

Implementing either of these approaches can help optimize performance by reducing redundant calls to $this->getPackageConfig().

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions