Skip to content
Raisalon

Software Engineering

Pagefind vs. JSON search for an Astro blog

Compare Pagefind and a JSON search index on 100 and 1,000 test pages. See first-query bytes, timings, and how to measure the trade-off on your Astro site.

Author
4 min read

A small Astro blog can ship one JSON (JavaScript Object Notation) file containing titles, descriptions and URLs and search it in the browser. Pagefind indexes the generated HTML and loads search assets as needed. The choice turns on two things: which words readers expect to find and how much data the browser needs to show the first result.

Compare the same query on two small collections

In a local test, both approaches returned /notes/42/ first for the query build note 42. The JSON approach transferred fewer bytes on both collections. Going from 100 to 1,000 documents added 108,003 bytes to the JSON response and 8,350 bytes to Pagefind’s first query. Those numbers describe a deliberately repetitive collection with very short descriptions.

Documents Search implementation Search response bodies Median time to first result
100 JSON index 11,677 bytes 3.9 ms
100 Pagefind 151,436 bytes 119.9 ms
1,000 JSON index 119,680 bytes 13.4 ms
1,000 Pagefind 159,786 bytes 123.2 ms

The test used Pagefind 1.6.0 and headless Chromium 159 over localhost. Each median comes from five runs in fresh browser contexts. Timing began with the query and ended when the first result URL was available. The server counted response bodies after navigation, including Pagefind’s runtime, index requests and first result data. The JSON scorer was already in the document. Page HTML, transport headers and rendering the result list were outside the measurement. The local server applied no HTTP compression or network throttling.

Understand what the fixture rewards

Document number 42 had the title Build note 42, the description Build timings for module 42, the URL /notes/42/ and terms build performance. Other documents used the same pattern with a different number. The JSON array used exactly these four fields, serialized without extra whitespace. The scorer from this blog required every query word to occur somewhere and weighted title hits above terms and descriptions.

Pagefind received an HTML document for each record with the same title, description and terms in the indexed body. Its search included loading the data for the first matching result. The following excerpt is the browser side of that query when Pagefind has generated its files at the site root. Pagefind JavaScript integration

const start = performance.now();
const pagefind = await import('/pagefind/pagefind.js');
const hits = await pagefind.search('build note 42');
const first = hits.results[0] ? await hits.results[0].data() : null;
console.log({ url: first?.url, milliseconds: performance.now() - start });

The ranking code and query timing answer a fairly narrow question. These documents share much of their vocabulary. Real articles have longer descriptions, code samples, names and misspellings. Compression can also be particularly effective on repetitive JSON. Extrapolating a crossover point from these two rows would give you a precise answer about the wrong collection.

Test the query your current search misses

Consider a post titled “Debug a date that changes after reload.” A reader remembers Asia/Kathmandu from its code. If your JSON contains only the title, description and tags and none mentions the zone, no ranking adjustment can find it. A body index can retrieve that page. Try several queries like this before investing time in shaving milliseconds off metadata search.

To try Pagefind on an Astro project, build the static site, then point its indexer at the output. Keep navigation, cookie notices and repeated footer text outside the searchable body. Where you mark article bodies explicitly, include all the pages that should be searchable. Pagefind indexing guide

npm run build
npx pagefind --site dist

On this blog the search page fetches its JSON index when the search component mounts. A reader may spend several seconds thinking before typing. If the download finishes during that pause, the first keystroke can feel instant even though the browser already paid the transfer cost. A benchmark that starts timing at the keystroke must account for that preload separately. The table above starts with a cold request so that the index download is visible.

For a production replay, open the search page in a fresh browser context and record total search-related bytes from navigation until the first result appears. Then type a different query and record additional bytes. Count worker requests as well as requests from the main document. For Pagefind, retrieving data for ten displayed results can cost more than loading the first one. Match the measurement to how many titles and excerpts your interface renders.

Finally, inspect the slowest useful query alongside the median. A rare name that returns one page and a common term that matches nearly every article exercise different parts of a search implementation. Keep the corpus, query list and result count with the measurements so you can repeat the comparison after your archive grows.

Next steps

Write down five queries that should work, including an exact title, a term from the middle of a post and a query with no match. Run both implementations on the same build. Record the result URL, first-query bytes and latency. Then repeat a second query in the same browser to measure the benefit of cached assets. If readers can already find everything they need from titles and tags, a compact JSON index is easy to maintain. If the missing results are buried in article text, measure full-text search against that requirement.

Share

Written by