LoginSignup
13
13

More than 5 years have passed since last update.

Laravelの環境判定で、 in_array(env('APP_ENV'), ['staging', 'production'], true) が最速。っぽい。

Last updated at Posted at 2017-03-17

Laravelで env('APP_ENV') を使うか、 App::environment() を使うか検討したメモ。

結論から言うと

コード 結果
in_array(env('APP_ENV'), ['dev', 'production'], true) Time: 428 ms, Memory: 14.00MB
(env('APP_ENV') === 'dev' or env('APP_ENV') === 'production') Time: 672 ms, Memory: 14.00MB
in_array(env('APP_ENV'), ['dev', 'production']) Time: 769 ms, Memory: 16.00MB
\App::environment('dev', 'production') Time: 3.89 seconds, Memory: 16.00MB

ファサードはめっちゃ遅いことはわかった。

第三引数 true にした in_array をこれからは積極的に使うことにする。

下記のテストを実施した。

tests/TimeTest.php
<?php

namespace Tests;

class FunctionsTest extends TestCase
{

    public function testInArrayStrict()
    {
        foreach (range(1, 100000) as $_) {
            in_array(env('APP_ENV'), ['dev', 'production'], true);
        }
        $this->assertFalse(in_array(env('APP_ENV'), ['dev', 'production']));
    }

    public function testEnvEvaluation()
    {
        foreach (range(1, 100000) as $_) {
            (env('APP_ENV') === 'dev' || env('APP_ENV') === 'production');
        }
        $this->assertFalse(env('APP_ENV') === 'dev' || env('APP_ENV') === 'production');
    }

    public function testInArrayLoose()
    {
        foreach (range(1, 100000) as $_) {
            in_array(env('APP_ENV'), ['dev', 'production']);
        }
        $this->assertFalse(in_array(env('APP_ENV'), ['dev', 'production']));
    }

    public function testFacade()
    {
        foreach (range(1, 100000) as $_) {
            \App::environment('dev', 'production');
        }
        $this->assertFalse(\App::environment('dev', 'production'));
    }

}
for method in testInArrayStrict testEnvEvaluation testInArrayLoose testFacade
  php vendor/bin/phpunit --filter $method tests/TimeTest.php
end

------------------------

.

Time: 428 ms, Memory: 14.00MB

OK (1 test, 1 assertion)
------------------------

.

Time: 672 ms, Memory: 14.00MB

OK (1 test, 1 assertion)
------------------------

..

Time: 769 ms, Memory: 16.00MB

OK (2 tests, 2 assertions)
------------------------

.

Time: 3.89 seconds, Memory: 16.00MB

OK (1 test, 1 assertion)

参考 http://qiita.com/tadsan/items/2a4c3e6b0b74a408c038

Qiitaでテーブルの中にパイプが書けなくてちょっとはまった。

13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13