Skip to content

Instantly share code, notes, and snippets.

@dshafik
Created May 19, 2024 01:40
Show Gist options
  • Save dshafik/0c94d445ef1432527ffaf57d4301f3cc to your computer and use it in GitHub Desktop.
Save dshafik/0c94d445ef1432527ffaf57d4301f3cc to your computer and use it in GitHub Desktop.
Simple Artisan Command for Benchmarking dshafik/bag against spatie/laravel-data
<?php
namespace App\Console\Commands;
use App\Values\BagValue;
use App\Values\SpatieValue;
use Illuminate\Console\Command;
use Illuminate\Support\Benchmark;
class ValueBench extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'benchmark:value {--iterations=1000}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Benchmark dshafik/bag vs spatie/laravel-data';
/**
* Execute the console command.
*/
public function handle()
{
Benchmark::dd([
'Bag' => function () {
$iterations = $this->option('iterations');
$i = 0;
while ($i < $iterations) {
BagValue::from([
'name' => 'Davey Shafik',
'mappedName' => 'Value',
'alias' => true,
'validatedInt' => 42,
'castDate' => '2021-01-01',
])->toArray();
$i++;
}
},
'Spatie' => function () {
$iterations = $this->option('iterations');
$i = 0;
while ($i < $iterations) {
SpatieValue::validateAndCreate([
'name' => 'Davey Shafik',
'mappedName' => 'Value',
'alias' => true,
'validatedInt' => 42,
'castDate' => '2021-01-01',
])->toArray();
$i++;
}
},
], 10);
}
}
<?php
namespace App\Values;
use Bag\Attributes\Cast;
use Bag\Attributes\MapInputName;
use Bag\Attributes\MapName;
use Bag\Attributes\Validation\Integer;
use Bag\Attributes\Validation\Required;
use Bag\Bag;
use Bag\Casts\DateTime;
use Bag\Mappers\Alias;
use Bag\Mappers\CamelCase;
use Bag\Mappers\SnakeCase;
use Carbon\CarbonImmutable;
#[MapName(input: SnakeCase::class, output: SnakeCase::class)]
readonly class BagValue extends Bag
{
public function __construct(
public string $name,
#[MapInputName(CamelCase::class)]
public string $mapped_name,
#[MapInputName(Alias::class, 'alias')]
public bool $aliasedName,
#[Integer]
#[Required]
public int $validatedInt,
#[Cast(DateTime::class, format: 'Y-m-d')]
public CarbonImmutable $castDate,
)
{
}
}
<?php
namespace App\Values;
use Bag\Attributes\MapName;
use Carbon\CarbonImmutable;
use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Attributes\Validation\IntegerType;
use Spatie\LaravelData\Attributes\Validation\Required;
use Spatie\LaravelData\Attributes\WithCast;
use Spatie\LaravelData\Attributes\WithTransformer;
use Spatie\LaravelData\Casts\DateTimeInterfaceCast;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Mappers\CamelCaseMapper;
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
use Spatie\LaravelData\Transformers\DateTimeInterfaceTransformer;
#[MapName(SnakeCaseMapper::class)]
class SpatieValue extends Data
{
public function __construct(
public string $name,
#[MapInputName(CamelCaseMapper::class)]
public string $mapped_name,
#[MapInputName('alias')]
public bool $aliasedName,
#[IntegerType]
#[Required]
public int $validatedInt,
#[WithCast(DateTimeInterfaceCast::class, format: 'Y-m-d')]
#[WithTransformer(DateTimeInterfaceTransformer::class, format: 'Y-m-d')]
public CarbonImmutable $castDate,
)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment