Should you rewrite your app in Rust, or only its slow functions?
Only the slow ones, after a fix in your own language: in one test a better TypeScript algorithm gave 3.3x before Rust added 2.9x.
Senior Full-Stack Engineer- Published
- 10 minReading time
- EngineeringCategory
Listen to this post· AI-narrated
0:00 / 12:04fast-anything (opens in a new tab) appeared on GitHub on 21 November with advice for the day you want to rewrite your app in Rust: "Don't rewrite it in Rust. Rewrite the 5% that's slow." It profiles your app, has a coding agent port only the slow function, and checks the port against recorded calls. Its TypeScript demo drops from 3.11s to 155ms, which is 20 times faster. On Hacker News the replies split: some said one slow 5% rarely exists, others asked how much of that gain is Rust. I could check the second question, so I took the demo apart and measured each change on its own. The first 3.3x needed no Rust at all.
TL;DR: Don't rewrite your app in Rust to make it faster. Profile it first. If one function dominates, fix it in your own language, then port it only if it is still too slow. In the TypeScript example I measured, a better algorithm gave 3.3x before any Rust, Rust added 2.9x, and eight threads added 7.5x.
Should you rewrite your app in Rust or only its slow functions?
Port only the slow functions, and only when a profile shows that they take most of the time. A rewrite of the rest can't help much, and Amdahl's law says exactly how much: the code you leave alone sets the limit. Say a Rust port makes one function 20 times faster. Here is what the whole run gains, by the share of time that function had before:
| Share of the run in that function | Whole run gets faster by |
|---|---|
| 95% | 10.3x |
| 80% | 4.2x |
| 50% | 1.9x |
| 30% | 1.4x |
| 10% | 1.1x |
Below half, the best you can get is under 2x, and for that you take on a Rust toolchain, a native build for every platform you ship to and a second language to review. That is the arithmetic behind the most useful reply in the thread (opens in a new tab): the strategy "only works if 5% of your software is the problem."
The same table argues against the full rewrite. If your app spends its time waiting on a database or an API, the language share is the 10% row, and Rust waits for Postgres exactly as long as Node does.
Where does a 20x Rust speedup come from?
A headline Rust speedup usually mixes three changes: a better algorithm, native code and more CPU cores. The fast-anything demo finds near-duplicates among 2,000 names with an edit-distance function. The TypeScript version compares every pair in full, on one thread. The reference Rust port (opens in a new tab) in the repository skips pairs whose lengths differ too much, trims the letters both names share at each end, reuses one buffer, and runs a thread per core. Of those, only the threads are hard to do in TypeScript.
So I wrote the missing steps. Every version ran on the same 2,000 names and returned the same 6,300 pairs:
| Version | Median time | Against the original |
|---|---|---|
| Original TypeScript | 1,978ms | 1x |
| TypeScript, one length check added | 1,521ms | 1.3x |
| TypeScript, same algorithm as the Rust port | 603ms | 3.3x |
| TypeScript, same algorithm, 8 worker threads | 154ms | 12.8x |
| Rust, same algorithm, one thread | 209ms | 9.5x |
| Rust, same algorithm, 8 threads | 28ms | 70.9x |
I ran each one 15 times on an M3 Max with Node 26.13 and rustc 1.99.1, with other work running on the machine. The Rust rows time a standalone binary, so they leave out the cost of calling Rust from Node, while the worker row includes starting the workers. The project measured on a different machine, so compare the steps and leave the totals alone.
The algorithm alone took 1,978ms to 603ms, with no new language. Rust took that to 209ms, a 2.9x gain on one thread, in line with Anders Hejlsberg, TypeScript's lead architect, who says JavaScript pays "a 2 to 3x perf penalty compared to native code" (opens in a new tab) on compute-heavy work. Eight threads took Rust to 28ms, another 7.5x; a thread per core, all 16, added nothing. TypeScript with eight workers beat Rust on one thread, 154ms to 209ms. Best against best, Rust still won by 5.6x, and that is the honest size of the port: 5.6x after the fix, where the headline says 20x before it.
How do you find out whether you have a slow 5%?
A CPU profile answers that in one run, and Node writes one without extra tools:
node --cpu-prof app.ts
Open the .cpuprofile file in the Performance panel of Chrome DevTools and sort the bottom-up view by self time. On the demo, levenshtein takes 84% of the self time and garbage collection another 7 to 8%, because the function allocates a new array for every row. That is the good case: one function, easy to isolate. Python has the same answer through cProfile or py-spy.
Plenty of profiles look different. A reply on Hacker News described them: "the true performance hog is like sand, spread across the system, a little bit in every place." (opens in a new tab) If your top function holds 8% and the next twenty hold 4% each, porting one function won't save you. Look for the work you repeat: the list you scan inside another loop, the JSON you parse twice, the component that renders on every keystroke. A port copies those mistakes faithfully.
If one function does dominate, fix it where it is. The first row I added to the table is one condition before the expensive call:
if (
Math.abs(clean[i].length - clean[j].length) <= maxDistance &&
levenshtein(clean[i], clean[j]) <= maxDistance
) {
pairs.push([i, j]);
}
Two names whose lengths differ by three can't be two edits apart, so the call never happens. The 3.3x row replaces levenshtein with the Rust port's method, in ordinary TypeScript. The names are stored as Uint16Array character codes:
// longest name + 1
const row = new Uint32Array(1024);
function distance(a: Uint16Array, b: Uint16Array): number {
let start = 0;
let aEnd = a.length;
let bEnd = b.length;
while (start < aEnd && start < bEnd && a[start] === b[start]) start++;
while (aEnd > start && bEnd > start && a[aEnd - 1] === b[bEnd - 1]) {
aEnd--;
bEnd--;
}
if (aEnd < bEnd) {
[a, b] = [b, a];
[aEnd, bEnd] = [bEnd, aEnd];
}
const length = bEnd - start;
if (length === 0) return aEnd - start;
for (let j = 0; j <= length; j++) row[j] = j;
for (let i = start; i < aEnd; i++) {
let diagonal = row[0];
row[0] = i - start + 1;
for (let j = 0; j < length; j++) {
const above = row[j + 1];
const change = a[i] === b[start + j] ? 0 : 1;
row[j + 1] = Math.min(above + 1, row[j] + 1, diagonal + change);
diagonal = above;
}
}
return row[length];
}
Before writing even that, search for a maintained package: for edit distance in Python, a commenter suggested RapidFuzz (opens in a new tab), which your team doesn't have to maintain.
When is it worth porting one function to Rust?
A port to Rust earns its place when the function is still the bottleneck after the fix, takes plain data, gives the same output for the same input, and does a lot of work per call. The last one decides most cases, because every call from Node into Rust has to hand the arguments over. fast-anything's README shows the cost on this same demo: porting the outer loop gave 20x, and porting only the inner levenshtein, which the app calls millions of times, gave 1.9x. Move the loop across, so the app enters Rust once.
Then prove the port. I'd copy fast-anything's check even without using the tool: it records up to 500 real calls, replays them against the Rust version and adds awkward cases such as empty text, emoji and NaN. In its own example a port that looked right compared bytes instead of characters, and 148 of the 500 recorded calls caught it. A coding agent will write you a convincing port in a minute. The recorded calls are the spec it has to pass, and I wouldn't enable one without them.
Work that grows with the input is where it pays. The maintainer of a Python web framework who moved routing and JSON encoding to Rust (opens in a new tab) reports that matching the last of 200 routes dropped to about half the CPU cycles, while serializing a large JSON fell by about 25%.
When does a full rewrite in Rust make sense?
Moving everything to Rust needs a reason bigger than one slow function: memory bugs, work that needs shared memory across threads, or a profile that is flat because the whole program is compute. Bun is the recent example. Its team moved 535,496 lines of Zig to Rust (opens in a new tab) after a run of use-after-free crashes and leaks, kept the architecture, and leaned on a test suite with a million assertions. TypeScript 7 is the other one. A compiler is compute all the way through, so the team ported it, to Go, and reports 8x to 12x faster full builds (opens in a new tab). It wanted native code and shared memory threads together, which are two of my three steps.
Both teams also had a test suite big enough to tell whether the new program behaves like the old one. JetBrains reviewed Rust rewrites in August and concluded: "Some projects are faster because of Rust, some just because they were rewritten from scratch." (opens in a new tab) The package manager uv is the familiar case of the second kind. Andrew Nesbitt's breakdown credits parallel downloads, a global cache and dropped legacy formats (opens in a new tab) ahead of the language.
Frequently asked questions
Is Rust faster than TypeScript?
On compute-heavy code, yes. With the same algorithm on one thread, Rust took a third of the time in my test. On code that waits for a network or a database, the difference mostly disappears, because neither language is doing the work while it waits.
Can worker threads replace a Rust port?
Sometimes. In my test eight workers beat single-threaded Rust. Sixteen were slower than eight, 186ms to 154ms, and each worker has to start up and receive its own copy of the names. Rust threads share memory, so they pay neither cost.
Is it safe to let a coding agent port code to Rust?
Check it against recorded inputs and outputs before you trust it, because nobody on your team may be able to review the Rust. Record real calls from production-like data, replay every one against the port, add edge cases, and keep the original one switch away.
What to do next
Run node --cpu-prof on your slowest job. If one function tops the list the way levenshtein did, write the two versions I needed before any Rust: the original with one cheap early exit, then the better algorithm in the same language. Time both against the same output. Now you know what Rust has left to win, which in my test was 2.9x on one thread and 5.6x with every core in play, and whether that is worth a native build. If nothing tops the list, spend the afternoon on the work your app repeats.
More posts on building software alone are on the gabs.app blog. I'm a senior full-stack engineer and my work is on gabs.app; if you're hiring, the contact form comes straight to me.