Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Refactor Kernel\OS\Common\Comparison::values() #37

@maximgrynykha

Description

@maximgrynykha

TL;DR

Performance: array_column is faster, leveraging PHP’s internal C implementation.
Readability: array_column is more concise and direct.

From

    /**
     * @return string[]|array<int, string>
     */
    public static function values(): array
    {
        return array_map(
            static fn (self $operator): string => $operator->value,
            self::cases()
        );
    }

To

    /**
     * @return string[]|array<int, string>
     */
    public static function values(): array
    {
        return array_column(self::cases(), 'value');
    }

array_map itself is implemented in C, but when combined with a custom closure, the closure introduces additional overhead because it executes PHP userland code for each element in the array.

array_column operates entirely within the optimized C layer, extracting values without requiring userland function calls, making it faster for this specific use case.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions