Is calling function_exists() faster or slower that apply_filters()

Is calling function_exists() faster or slower that apply_filters() … or is the difference so small that it should not be considered?

I did a bit of testing based on Kaiser’s and it showed that function_exists() is ~3 times faster where both the function and filter exist. and ~11 times faster if the filter does not exist. Was not expecting this.

Read More
function taco_party() {
    return true;
}

add_filter( 'taco-party', 'taco_party' );

timer_start();
for ( $i = 0; $i < 1000000; $i++ )  {
    $test = apply_filters( 'taco-party', '' );
}
echo( 'Seconds: ' . timer_stop( 0, 10 ) . '<br />' );

timer_start();
for ( $i = 0; $i < 1000000; $i++ )  {
    if ( function_exists( 'taco_party' ) ) {
        $test = taco_party();
    }
}
echo( 'Seconds: ' . timer_stop( 0, 10 ) . '<br />' );

Bear in mind that this is running each method 1,000,000 times which is quite a lot. Each method ran once completes very, very quickly:

Test 1: 0.0000491142
Test 2: 0.0000140667

I would conclude that the difference is not an issue.

Related posts

Leave a Reply

4 comments

  1. I don’t know if one is faster or slower than the other, but I would suggest that using apply_filters() is the better approach, because it is cleaner, and more intuitive (in the sense of doing things the “WordPress way”).

    EDIT

    If you’re doing comparison tests, shouldn’t you compare the time required to execute the following:

    This:

    <?php
    if ( ! function_exists( 'taco_party' ) ) {
        function taco_party( $salsa = true ) {
            return $salsa;
        }
    }
    
    function taco_party( $salsa = true ) {
        return $salsa;
    }
    ?>
    

    Versus This:

    <?php
    function taco_party( $salsa = true ) {
        return apply_filters( 'taco-party', $salsa );
    }
    function hot_salsa() {
        $salsa = true;
        return $salsa;
    }
    add_filter( 'taco-party', 'hot_salsa' );
    ?>
    

    It’s not just the time required to check for the existence of the function or filter, but rather the time required to do something.

  2. Disclaimer: The numbers are out of a non-vanilla install, so it could be less on both sides

    WordPress has ~ 2.700 functions & ~ 250 attached actions in global $wp_filter;

    So the Q could be:

    Is iterating through the (at this point) existing functions faster than iterating through the array of hooked filters?

    Imo filters/hooks are the faster way, more safe and easier to backtrace. My choice would be filters.

  3. Ok, after running several tests1) I can see that has_filter takes ~ 20% more time to finish (on a not vanilla install) than function_exists:

    // You have to uncomment one or the other to run the test
    // don't run timer_xy() functions behind each other - the result is then wrong
    timer_start();
    $output = ' false ';
    for ( $i = 0; $i < 1000000; $i++ ) 
    {
        // if ( function_exists('wp_login') ) $output = ' true ';
        if ( has_filter( 'shutdown' ) ) $output = ' true ';
    }
    echo $output;
    echo('Seconds: ' . timer_stop(0,10) . '<br />');
    

    1) This is the code I used to test. It was set on the top of the functions.php file

  4. You are talking about micro-optimizations here. Stick to apply_filters which is easier to use and allows the end user to also add content before/after your function output if they wanted without copying the entire function over if that’s all they want to do. Don’t be so concerned about possibly saving 0.000001 seconds on anything. Always focus on clean, easy to read and modular code. PHP keeps getting faster with each update and WordPress sites should be using caching functions anyway to return static code so micro-optimizing is just a waste of time and stress 😉