Giter Club home page Giter Club logo

Comments (4)

butschster avatar butschster commented on September 2, 2024 2

Here is a benchamark code

<?php

declare(strict_types=1);

namespace App\Endpoint\Console;

use Illuminate\Support\Arr;
use Spiral\Console\Command;
use DragonCode\Benchmark\Benchmark;
use Spiral\Http\Request\InputBag;
use Yiisoft\Arrays\ArrayHelper;

class BenchCommand extends Command
{
    protected const NAME = 'bench';
    protected const DESCRIPTION = '';

    private const ARR_PATH = 'foo.quux.corge.grault.garply.xyzzy.quux.corge.grault.garply.xyzzy.quux.corge.grault.garply.xyzzy.quux.corge.grault.garply.xyzzy.thud';

    protected function perform(): void
    {
        $array = $this->getArray();

        $bag = new InputBag($array);

        (new Benchmark())
            ->iterations(100000)
            ->withoutData()
            ->compare([
                'Spiral' => fn () => $bag->get(self::ARR_PATH),
                'Yii' => fn () => ArrayHelper::getValueByPath(
                    $array,
                    self::ARR_PATH,
                ),
                'Laravel' => fn () => Arr::get(
                    $array,
                    self::ARR_PATH,
                ),
            ]);
    }

    private function getArray(): array
    {
        return [
            'foo' => [
                'bar' => [
                    'baz' => 'qux',
                ],
                'quux' => [
                    'corge' => [
                        'grault' => [
                            'garply' => [
                                'waldo' => [
                                    'fred' => 'plugh',
                                ],
                                'xyzzy' => [
                                    'quux' => [
                                        'corge' => [
                                            'grault' => [
                                                'garply' => [
                                                    'waldo' => [
                                                        'fred' => 'plugh',
                                                    ],
                                                    'xyzzy' => [
                                                        'quux' => [
                                                            'corge' => [
                                                                'grault' => [
                                                                    'garply' => [
                                                                        'waldo' => [
                                                                            'fred' => 'plugh',
                                                                        ],
                                                                        'xyzzy' => [
                                                                            'quux' => [
                                                                                'corge' => [
                                                                                    'grault' => [
                                                                                        'garply' => [
                                                                                            'waldo' => [
                                                                                                'fred' => 'plugh',
                                                                                            ],
                                                                                            'xyzzy' => [
                                                                                                'thud' => 'Hello world!',
                                                                                            ],
                                                                                        ],
                                                                                    ],
                                                                                ],
                                                                            ],
                                                                        ],
                                                                    ],
                                                                ],
                                                            ],
                                                        ],
                                                    ],
                                                ],
                                            ],
                                        ],
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
            'garply' => [
                'waldo' => [
                    'fred' => 'plugh',
                ],
                'xyzzy' => [
                    'thud' => 'thud',
                ],
            ],
            'thud' => [
                'thud' => [
                    'thud' => 'thud',
                ],
            ],
        ];
    }
}

And results

image

from arrays.

vjik avatar vjik commented on September 2, 2024

Seems, problem here:

https://github.com/yiisoft/strings/blob/b35142c92c8108a872b0ed5d83d2c62426a056dd/src/StringHelper.php#L538

It's allow escape separate symbols. For example:

array: ['x']['a.b']['c']
path: x.a\.b.c

For boost may prepare key via explode('.', self::ARR_PATH).

from arrays.

samdark avatar samdark commented on September 2, 2024

Need to try this:

<?php

$key = 'x.a\.b.c';

$parts = splitIt($key);
var_dump($parts);

function splitIt(string $key, string $delimiter = '.', $escape = '\\')
{
    $prev = 0;
    $parts = [];
        
    for ($i = 0, $len = strlen($key); $i < $len; $i++) {
        if ($key[$i] === $delimiter && ($i === 0 || $key[$i-1] !== $escape)) {
            $parts[] = str_replace($escape, '', substr($key, $prev, $i - $prev));
            $prev = $i + 1;
        }
    }
    $parts[] = substr($key, $prev, $i - $prev);
    return $parts;
}

from arrays.

samdark avatar samdark commented on September 2, 2024

Also

$array = preg_split('~\\\\.(*SKIP)(*FAIL)|\|~s', $string);

See https://stackoverflow.com/questions/6243778/split-string-by-delimiter-but-not-if-it-is-escaped

from arrays.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.