Performance: FFI (Foreign Function Interface) for native code
FFI affects page load speed and runtime responsiveness by introducing native code calls that can block or delay JavaScript execution.
const lib = Deno.dlopen("./libnative.so", { compute: { parameters: ["i32"], result: "i32" } }); const result = await Promise.resolve().then(() => lib.symbols.compute(42));
const lib = Deno.dlopen("./libnative.so", { compute: { parameters: ["i32"], result: "i32" } }); const result = lib.symbols.compute(42);
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous FFI call on main thread | 0 | 0 | Blocks paint until native returns | [X] Bad |
| Asynchronous or worker-based FFI call | 0 | 0 | Non-blocking paint and input | [OK] Good |
Deno.dlopen in Deno's FFI?Deno.dlopenDeno.dlopen is used to load native shared libraries (like .dll, .so, .dylib) so Deno can call native functions.Deno.dlopen?Deno.dlopen require specifying parameter types and the return type. For a function taking an integer and returning an integer, both parameters and result should be "i32".const lib = Deno.dlopen("./libmath.so", {
add: { parameters: ["i32", "i32"], result: "i32" }
});
const result = lib.symbols.add(10, 5);
console.log(result);
lib.close();const lib = Deno.dlopen("./libcalc.so", {
multiply: { parameters: ["i32", "i32"], result: "i64" }
});
const output = lib.symbols.multiply(3, 4);
console.log(output);
lib.close();Deno.UnsafePointerView to read UTF-8Deno.UnsafePointerView can read the memory as a UTF-8 string safely.Pass the test and this pattern moves unknown