[wp-trac] [WordPress Trac] #58587: Performance of in_array

WordPress Trac noreply at wordpress.org
Wed Jun 21 08:29:21 UTC 2023


#58587: Performance of in_array
-------------------------------------------+------------------------------
 Reporter:  benniledl                      |       Owner:  (none)
     Type:  enhancement                    |      Status:  new
 Priority:  normal                         |   Milestone:  Awaiting Review
Component:  General                        |     Version:
 Severity:  minor                          |  Resolution:
 Keywords:  has-testing-info dev-feedback  |     Focuses:  performance
-------------------------------------------+------------------------------

Comment (by swissspidy):

 Thanks a lot for doing this analysis and opening this ticket with some
 numbers attached!

 Indeed it looks like in this isolated benchmark replacing this simple
 `in_array()` check with an if statement brings a slight performance gain.

 However, the difference is only 1ms for 100 million iterations. In
 reality, `get_site_transient()` (where you spotted this) is never called
 that many times. And with 100k iterations (which is still way more than
 what you would see on a page load) the difference is a couple of
 nanoseconds, which is basically nothing.

 {{{
 in_array 0.00416

 if && 0.00434

 if else 0.00238
 }}}

 (From https://3v4l.org/Cb6q4#v8.2.7)

 The fewer iterations, the smaller the difference of course.

 In other words, on an actual page load in a real world scenario you would
 not see any difference at all. So, this change is not really worth doing
 IMHO, especially since it decreases readability.

 That said, `in_array()` with hardcoded haystack is used frequently in
 core. If it's used somewhere else in a hot code path, then that would be
 more interesting to look into.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58587#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list