I Moved One Hot Function From Node to Rust With napi-rs
A CSV import that blocked the event loop for seconds. How I moved the parsing to a Rust addon, what it cost in build complexity, and the JavaScript fix I should have tried first.
All the Rust in my stack so far came from other people: bundlers, linters, formatters. This spring I wrote some myself, for one function in a Node backend. It worked, and I learned more from what came before it than from the Rust itself.
The problem
An admin feature imports large CSV files: tens of thousands of rows, each parsed, validated, and normalized before it's written to the database. The parsing ran on the main thread. While a big file was being processed, every other request to that service waited. Health checks timed out during imports.
What I should have tried first
Before any Rust, two JavaScript fixes, in this order:
- Stream instead of loading the whole file. Parse row by row, write in batches. Memory stays flat and the event loop gets breathing room between chunks.
- Move the work off the main thread. A
worker_threadsworker keeps the server responsive even if parsing itself stays slow.
I did both. The service stopped freezing, which was the actual bug. Imports were still slow, and the import runs often enough that slow mattered. That's when Rust became worth trying, not before.
The Rust side
napi-rs generates the Node bindings from a macro. The exported function takes the raw bytes and returns validated rows plus errors:
use napi_derive::napi;
#[napi(object)]
pub struct ParsedRow {
pub sku: String,
pub qty: u32,
pub price_cents: i64,
}
#[napi(object)]
pub struct ParseResult {
pub rows: Vec<ParsedRow>,
pub errors: Vec<String>,
}
#[napi]
pub fn parse_import(input: napi::bindgen_prelude::Buffer) -> ParseResult {
let mut rows = Vec::new();
let mut errors = Vec::new();
let mut reader = csv::Reader::from_reader(input.as_ref());
for (i, record) in reader.records().enumerate() {
match record.map_err(|e| e.to_string()).and_then(|r| validate(&r)) {
Ok(row) => rows.push(row),
Err(e) => errors.push(format!("row {}: {}", i + 2, e)),
}
}
ParseResult { rows, errors }
}From Node it looks like any other function, with TypeScript types generated for it:
import { parseImport } from "@acme/import-parser";
const { rows, errors } = parseImport(await readFile(path));I still call it from the worker thread. Fast code on the main thread is still code on the main thread.
What it cost
The function was the easy part. The real cost was everything around it:
- Prebuilt binaries per platform. Developers on macOS, CI on Linux x64, production on Linux arm64. napi-rs scaffolds a GitHub Actions matrix that builds each one and publishes them as optional packages. It works, but it's a pipeline to own.
- A second language in code review. Not everyone on the team reads Rust. I kept the crate small and commented every non-obvious line.
- Crossing the boundary. Passing one buffer in and one result out is cheap. Calling into Rust once per row would have thrown the gain away. Design the API so it crosses once.
Was it worth it?
For this one function, yes: imports are now fast enough that nobody asks about them. For the next slow function, I'll go through the same order again. Stream, move it off the main thread, profile, and only then reach for Rust. Most of the time the first two steps are the whole fix.
Keep reading
All posts2026
6 min
Drizzle, Two Years Later: Now the Default in Every Backend I Start
Drizzle is close to 1.0 and Prisma dropped Rust, so speed isn't the argument anymore. The reason I still start every Node backend with Drizzle is simpler. It's SQL, with types.
databasebackend3 min
Oxlint and Oxfmt Next to Biome, and Which One I Kept
Oxfmt hit beta in February and Oxlint keeps growing. Both are Rust, both are fast. A side-by-side on a real project, and why I kept Biome on most repos but not all of them.
rusttooling