Skip to content

perf: speed up padBytes, padHex, concatHex, trim, sliceHex and size - #5537

Closed
jxom wants to merge 3 commits into
mainfrom
perf/data-utils
Closed

jxom wants to merge 3 commits into
mainfrom
perf/data-utils

Conversation

@jxom

@jxom jxom commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

From the sync encoding primitives section of a performance and memory audit. Every change keeps outputs and errors identical; see Behavior.

Summary

  • padBytes copies with Uint8Array.set instead of a per-index loop. Sizes that aren't safe integers (NaN, fractions) keep the loop, because it skips the fractional indices they produce. Empty views skip set, which throws on detached buffers.
  • concatHex builds the result in a plain loop instead of reduce.
  • trim compares char codes and bytes directly instead of calling .toString() on every element, with separate loops for hex and bytes.
  • padHex, sliceHex, concatHex, trim strip a leading 0x with slice(2), falling back to replace('0x', '') when the value doesn't start with it, so values without a prefix behave as before.
  • size checks for the 0x prefix with typeof and startsWith, which is what isHex(value, { strict: false }) did.

Behavior

Unchanged. A harness compared main and this branch on 103,377 calls on Node 26, Node 24 and Bun 1.4: identical return values and identical thrown errors (class and message). Inputs included padHex, padBytes and pad with sizes null, 0, -1, 1.5, NaN and an unknown dir, concatHex with values without a prefix or with 0x in the middle, trim in both directions, all-zero and odd-length values, sliceHex and slice with negative, reversed and fractional offsets in strict mode, size on non-strings, and detached views.

New tests (snapshots recorded on main) cover padBytes with a detached view and with NaN and fractional sizes (the per-index fallback), plus values without a 0x prefix in padHex, concatHex, sliceHex and size, and trim edge cases.

Benchmarks

SKIP_GLOBAL_SETUP=true vitest bench --project core --run <file>, Node 26.13, same bench file on main and this branch. The orb is shared, so expect ±10–20% noise.

Bench main (ops/s) branch (ops/s) ×
padHex 4 B → 32 3,092,109 5,647,087 1.83
padBytes 4 B → 32 2,383,675 3,498,499 1.47
padBytes 20 B → 32, right 2,972,855 6,144,746 2.07
trim 32 B hex 668,099 1,269,186 1.90
trim 32 B bytes 784,043 1,521,587 1.94
concatHex 10 × 32 B 1,323,848 4,134,552 3.12
concatHex 1000 × 32 B 27,545 59,817 2.17
sliceHex 1 KB, 4 to 36 5,091,985 8,875,271 1.74
size 32 B hex 10,686,017 13,279,477 1.24

In a standalone loop, trim on 32 bytes goes 1.59M → 10.0M ops/s. A smaller patch that only replaced .toString() === '0' and kept the shared loop reached 1.7M, which is why bytes and hex now have separate loops.

New bench files: concat.bench.ts, slice.bench.ts, size.bench.ts. pad.bench.ts and trim.bench.ts gained cases.

Size

Entry main this PR all five PRs limit
import * from 'viem' (esm) 91,183 B 91,272 B (+89) 91,881 B (+698) 92 kB
const viem = require('viem') (cjs) 117,921 B 118,099 B (+178) 118,541 B (+620) 118.5 kB → 119 kB
import * from 'viem/account-abstraction' 61,497 B 61,670 B (+173) 62,072 B (+575) 62 kB → 62.5 kB
import { privateKeyToAccount } from 'viem/accounts' 21,696 B 21,760 B (+64) 22,122 B (+426) 22 kB → 22.5 kB
import * from 'viem/actions' 72,912 B 73,040 B (+128) 73,494 B (+582) 73.5 kB → 74 kB

This PR fits the current limits on its own (pnpm size with time measurement off, since there's no Chrome here). All five PRs in this series together go over three limits by 41–122 B and leave viem/actions 6 B under its limit. So every PR in the series raises those four limits by 0.5 kB in a separate chore: commit (precedent: 9677703). The edit is identical in each PR, so git merges them cleanly in any order, and no intermediate merge state breaks pnpm size.

Series: #5534 (bytesToHex, stringToHex, stringToBytes), #5535 (hexToBytes, toBytes), #5536 (number conversions), #5537 (data utils), #5538 (TextDecoder, normalizeSignature, formatUnits). They touch some of the same files in separate hunks; any merge order applies cleanly (checked both orders).

Checks

  • Equivalence harness (main vs branch): 103,377 calls, 0 differences on Node 26, Node 24 and Bun.
  • pnpm test --project core --run src/utils/data: passing.
  • src/utils, src/accounts, src/siwe: the only failures are the Anvil-dependent tests that fail identically on main here (no Anvil in this environment).
  • Line and branch coverage of the changed modules on Node 26 is the same as on main: full, apart from two branches that were already uncovered (slice.ts L67, Value.ts L29).
  • Biome clean; tsc clean for src and the changed test and bench files.
  • pnpm knip --production clean on the five PRs merged together.

@vercel

vercel Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
viem Error Error Nov 16, 2026 2:09am UTC

Request Review

@changeset-bot

changeset-bot Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a47f352

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
viem Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T02:07:33.611290Z a47f352 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Published [email protected] under the pr-5537 dist-tag, which follows this pull request until it closes.

pnpm add viem@pr-5537
pnpm add [email protected]

jxom commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

Bundled into #5543 as its own commit, so this PR is superseded. Closing in favour of the bundle.

@jxom jxom closed this Nov 16, 2026

This branch had an error being deployed

1 failed deployment
Preview — a47f352e Deployed Nov 16, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants