Comments (4)
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
from arrays.
Seems, problem here:
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.
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.
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)
- Unexpected default value HOT 1
- ArrayableTrait: toArray doesn't resolve array members when no specific fields/expand is provided
- ArrayHelper::getObjectVars() description seems to be out of date HOT 2
- PHP 8 compatibility with \ArrayAccess
- Adjust input and output for `ArrayHelper::parsePath()` HOT 1
- How to create an array of strings in c language?
- Fix tests HOT 2
- Get array value by matcher HOT 1
- Extend the functionality of setValue HOT 3
- update irc link
- update src folder link
- Remove scrutinizer
- Psalm 5.23.1
- Increase MSI to 100
- Remove ArrayHelper::isTraversable
- Problem with support float as keys HOT 2
- ArrayHelper::multisort: remove support use \Closure with return multiple keys HOT 2
- call getter for property if getter exists. HOT 5
- Why methods like map expect array in first parameter instead of iterable HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arrays.