Parameterized queries and encoded output are the safest way to build pages; ZenDB does both automatically, at a cost measured in microseconds. This page shows the measurements: complete pages for a sample news site, each timed both ways, with the query and every HTML encode included on both sides.
Pages that HTML-encode their output, the common case.
| Page | raw mysqli + htmlspecialchars() |
ZenDB + SmartString | difference |
|---|---|---|---|
| Detail, 1 article | 0.048 ms | 0.045 ms | -0.000003 s |
| Widget, 5 rows | 0.066 ms | 0.076 ms | +0.000010 s |
| List, 25 rows | 0.145 ms | 0.164 ms | +0.000019 s |
| List, 100 rows | 0.447 ms | 0.531 ms | +0.000084 s |
Data processing with no HTML output: ZenDB's ->toArray() returns plain
arrays and skips the encoding layer.
| Page | raw mysqli | ZenDB ->toArray() |
difference |
|---|---|---|---|
| Detail, 1 article | 0.034 ms | 0.038 ms | +0.000004 s |
| List, 25 rows | 0.118 ms | 0.136 ms | +0.000018 s |
| List, 100 rows | 0.334 ms | 0.410 ms | +0.000076 s |
Worst case, ZenDB adds 84 millionths of a second to a page. Best case, it's faster: the single-article detail page runs faster through ZenDB than through hand-written mysqli. For scale, humans start to notice interface delays around 100 ms: about 1,200 times the largest difference in these tables.
These benchmarks ran on PHP 8.5 and MariaDB 12.3 on a dedicated Xeon E-2386G with the database on the same machine; two full passes agreed within 1%, and PHP 8.1 through 8.5 come out within a few percent of each other. Slower hosting scales every column up roughly proportionally.
If those numbers answer your question, you can stop here. The rest of the page shows the code behind each column, explains where the microseconds go, and ends with how to rerun everything.
Contents:
- How We Measured
- The List Page, Two Ways
- Why It Comes Out This Way
- When to Care
- The Fine Print
- Reproducing the Numbers
The corpus is a news table: 1,000 rows of realistic prose across 10 categories, each row a 60-byte title with an apostrophe, a 300-byte summary, and 5 KB of content.
CREATE TABLE news
(
id INT PRIMARY KEY AUTO_INCREMENT,
category VARCHAR(30) NOT NULL,
title VARCHAR(255) NOT NULL,
summary TEXT NOT NULL,
content MEDIUMTEXT NOT NULL,
created_at DATETIME NOT NULL,
KEY idx_category_created (category, created_at)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;Each cell in the tables above times the complete page: run the query, fetch the rows, HTML-encode every output. The raw-array cells skip the encoding on both sides.
The 25-row list page from the tables above: load the 25 newest articles in a category, output title and summary per row.
$category = $_GET['category'] ?? 'news';
$query = "SELECT * FROM news WHERE category = '" . $mysqli->real_escape_string($category) . "'
ORDER BY created_at DESC LIMIT 25";
$rows = $mysqli->query($query)->fetch_all(MYSQLI_ASSOC);
foreach ($rows as $row) {
echo "<h2>" . htmlspecialchars($row['title'], ENT_QUOTES | ENT_SUBSTITUTE | ENT_DISALLOWED | ENT_HTML5, 'UTF-8') . "</h2>";
echo "<p>" . htmlspecialchars($row['summary'], ENT_QUOTES | ENT_SUBSTITUTE | ENT_DISALLOWED | ENT_HTML5, 'UTF-8') . "</p>";
}$category = $_GET['category'] ?? 'news';
$articles = DB::query("SELECT * FROM news WHERE category = ? ORDER BY created_at DESC LIMIT 25", $category);
foreach ($articles as $article) {
echo "<h2>$article->title</h2>"; // output auto-HTML-encodes
echo "<p>$article->summary</p>";
}ZenDB is the shorter of the two, and there is nothing to escape, nothing to wrap, and nothing to remember per field.
One finished query per call. ZenDB escapes every value and inlines it into the SQL before sending, so each query is a single round trip with no prepare step. Sending finished SQL is just as safe: the API takes placeholders only, and SQL templates with inline values are rejected. See Security Gotchas for the full reasoning.
Long fields encode faster through SmartString. SmartString's encoder
scans first and only transforms text that needs it, so on multi-KB fields it
runs faster than htmlspecialchars() (measurements on
SmartString's performance page).
That saving on the 5 KB content field offsets ZenDB's query-building
work on the detail page: ~4 µs behind on raw arrays, ~3 µs ahead with HTML
output.
Row construction sets the list-page gap. Every row is wrapped in a SmartArray object, so the difference grows with row count. The raw-array table is the clean view, no encoding on either side: ~4 µs at 1 row, ~18 µs at 25 rows, ~76 µs at 100. The HTML list gaps run a little larger (~19 µs at 25 rows, ~84 µs at 100): titles and summaries are short fields, so the long-field encoding saving doesn't apply there.
Rarely. The largest difference on any page, ~84 µs (0.000084 s), is about a sixth of that page's own ~0.53 ms cost and about a 1,200th of the ~100 ms where humans notice. These tests also ran with the database on the same machine as PHP; when the database is a separate server, network time is added equally to every column, so the differences stay the same size and shrink as a share of each page. If a page is genuinely slow, benchmark it and fix what the numbers show: it will be a slow query, a missing index, or an API call, not ZenDB.
What the microseconds buy is visible in the code above: every value parameterized and every output encoded without anyone remembering to do it. With raw SQL you escape every string and wrap every output on every page, indefinitely, so a page is only as safe as its most recently edited line.
Benchmark choices, stated plainly.
- Both sides are timed in full. Every cell times the query plus all
output work, and the baseline is raw SQL as a careful programmer writes
it: escaped values,
htmlspecialchars()on every output. - The benchmark checks correctness before timing. Before timing starts,
the suite verifies ZenDB's HTML output for the 25-row list page is
byte-identical to the baseline's
htmlspecialchars()output, using the exact flags shown in the code above. - The baseline encodes with the full flag set. ZenDB encodes with
ENT_QUOTES | ENT_SUBSTITUTE | ENT_DISALLOWED | ENT_HTML5for the extra safety:ENT_DISALLOWEDreplaces control characters that are invalid in HTML, so they never reach the page. The baseline uses the same flags to produce identical output. Plainhtmlspecialchars($x)with PHP's defaults skips that scan and encodes these pages ~3 µs (detail) to ~23 µs (100-row list) faster; against that baseline the gaps grow by the same amounts and the detail page comes out about even. - PHP's JIT compiler is off, matching PHP's default production configuration.
- The corpus is shaped like real pages. 1,000 rows across 10 categories, with special characters as often as in real prose; the same corpus SmartArray benchmarks against.
- Ties are real ties. Every run includes a raw-vs-itself calibration cell that must measure ~1.00x, and the two full passes behind these tables agreed within 1% per cell.
- Scope. All cells are reads on a dedicated Linux x64 server (Intel Xeon
E-2386G), PHP 8.5, MariaDB 12.3 on the same machine, with no
encryptionKeyset. Write paths and other platforms were not measured. The same workflow dispatched on GitHub Actions (PHP 8.1-8.5, MySQL 8.0) comes out within about 10 percent on every ratio.
The tables come from the repo's benchmark workflow, which builds the news
corpus, runs the correctness check, and times every cell pairwise in one
process. Run its probe script locally against your own MySQL. The script
connects to 127.0.0.1 as root with no password, uses the
phpunit_test_db database (creating it if needed), and rebuilds a
zenbench_news table there; set DB_HOSTNAME, DB_PORT, DB_USERNAME,
DB_PASSWORD, or DB_DATABASE to change that:
git clone https://github.com/interactivetools-com/ZenDB.git
git clone https://github.com/interactivetools-com/SmartString.git
git clone https://github.com/interactivetools-com/SmartArray.git
cd ZenDB && composer install
php .github/scripts/escape-zendb-probe.phpThe published numbers come from this probe on the dedicated server described
in The Fine Print, run twice with the passes agreeing
within 1%. You can also fork the repo and dispatch the same workflow on
GitHub Actions: gh workflow run escape-zendb-matrix.yml. The committed
grids for every benchmark family are in
escape-results.md.
← Security Gotchas | Documentation Index | Next: Troubleshooting →