Many experienced developer knows that function defined()
is arround 19 times slower than any other checks.
I got in the problem where I need to have loop with over 5000 records and 7 defined()
checks in the sequence and that slowdown complete project.
Changing a way of project work or chunking is not possible in this case. Is there some good solution or idea how to avoid defined()
function?
Did you try using
constant ( string $name ) : mixed
Returns the value of the constant, or NULL if the constant is not defined.
Given that in php null == false
if (defined($name))
and
if (constant($name))
are almost identical
https://www.php.net/manual/en/function.constant.php
I test something like:
if(!defined('TEST')) continue;
and
if(!@TEST) continue;
and
if(NULL === @constant('TEST')) continue;
My measures on the PHP 7.1 is that if(!@TEST) continue;
is faster than if(NULL === @constant('TEST')) continue;
but both is slower than if(!defined('TEST')) continue;
It seams that we not have faster solution yet and define()
is still the faster way to check constants.