PHP Performance and Kirby

I’m building a site that will mostly be browsed through searching and filtering. The pages searched will all be books, and the books will have attributes such as:

  • page count
  • year published
  • original language

I’ve created a search module that posts the search parameters to a PHP script via AJAX. This does two things: as you update values in the search filter, it returns only the number of results (change Max Pages from 100 to 200, “5 Results” updates to “20 Results”). The actual search returns all matching

I’ve created a search module that posts the search parameters to a PHP script via AJAX. The script does two things:

  • As you make changes to the search module, the number of results is listed below.
  • When you’ve made your selections (and the number of results is above 0), you can click “Search” and you’ll get the real results

The search function looks something like this:

define('DS', DIRECTORY_SEPARATOR);
require($_SERVER["DOCUMENT_ROOT"] . DS . 'kirby' . DS . 'bootstrap.php');
$kirby = kirby();
$site = $kirby->site();

$params = $_POST;
echo "<br><br>";
foreach($params as $param => $value) {
	switch ($param) {
		case "min_pages":
			$books = $books->filterBy('page_count', '>', $value);
			break;
		case "max_pages":
			$books = $books->filterBy('page_count', '<', $value);
			break;	
		case "decade":
			$max = (string)(intval($value) + 10);
			$books = $books->filterBy('year_published', '>=', $value)->filterBy('year_published', '<', $max);
			break;	
	}
}

// echo some things back for the AJAX response

It was simple to put together and is working wonderfully.

But, I’m a little clueless about performance. Every time a user changes a value, the whole script gets called. This could happen fairly quickly, and a handful of times before the final search.

I thought of one way to lighten this load:

  1. Create a “book index” file that is a JSON object with only the relevant book search data.
  2. Update this index every time changes are made in the panel via the new Hooks feature.
  3. Have the search function get_file_contents from the index file instead of loading up Kirby and using its API every single time it is called.

Would this be a remarkable savings of server brainpower? Should I bother? Are there smarter ways to do this? Any tips on benchmarking?

This site isn’t going to be getting large amounts of traffic - but I need to start getting smarter about this kind of thing as I learn more.