2

If i have a plugin that contains 1 single php file and another plugin that contains 100 php files, 50 images, some css/js etc. Will the actual quantity of the files within this plugin affect the performance. I know the quality of the code matters, but in this case i want to know does wordpress scan all files in a plugin directory or does it only load the main {pluginname}.php file?

For example, a plugin with 100 php files, none of these files are included within the main {pluginname}.php file. Does this mean wordpress will just ignore all of these files and it wont do anything for performance?

1 Answer 1

7

You're worrying too much about something that doesn't mean much. WordPress doesn't care how many PHP files you have; the PHP interpreter does. And the PHP interpreter really doesn't care about the number of files until it actually accesses those files and executes the code. And for all purposes, the interpreter doesn't access a file until told by the code.

Modern servers and PHP are so fast that you're not going to see an appreciable difference in the speed that the interpreter reads files. With 10,000 files, maybe there will be a slowdown. But that's more the server, the OS, the storage (HD or SSD), RAM, etc., dealing with the files themselves and the directory structure not the PHP code in each file.

Now, what is in those PHP files will make the difference. The speed of the PHP interpreter depends on the CPU of the server. Slow shared server = slow PHP execution. And if you're trying to calculate pi with PHP, you're find out that PHP will use lots of CPU and will be slow, depending on the server.

And if you're using PHP to call the database with complex queries, or calling external resources that depend on other servers, those are more performance hits having to do with PHP execution and not the number of files.

Yes, a plugin can be slow, even with only one PHP file, if you're doing extravagant things in that one file. The PHP code and what it does is the issue, not the raw number of PHP files. The files - and the code in them - just sit there, waiting to be executed; the execution is what takes the time and impacts the performance.

WordPress will load the main PHP of a plugin, and then PHP does what it is told to do in that one file; and if that main PHP file tells PHP to load 1,000 more files, and there are executable hooks, database calls, etc., in each file, then yes, the plugin could be slow.

Edit: And, as pointed out in comments, I'm not even taking PHP caching into account, and that can make a huge difference in performance of the OS, file system and the PHP interpreter.

4
  • well my worry was that the code within the wordpress core that loads the plugin was going to do some sort of loop to scan every file in a plugins folder in order to check for hooks or something. Based on your answer its safe to assume that wordpress does not need to scan the contents of the directory. it loads the main plugin file and thats it? Commented Sep 27, 2016 at 16:18
  • WP loads the main plugin PHP file and PHP does what that file says; if that PHP file says load 1000 other PHP files and there are executable hooks in each one, yes, it could be slow. Commented Sep 27, 2016 at 16:36
  • since php 5.6 interpreter results are cached so files are not even going to be read again if their time stamp matches the last time the were read.In addition in most modern OS files which are frequently read will be in a memory cache, so even the HDD might not need to do anything Commented Sep 27, 2016 at 17:08
  • Good point about caching; I didn't think about that. Commented Sep 27, 2016 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.