Back

Oliver Medhurst

Porffor FFI

Porffor now has experimental FFI support!

FFI (foreign function interface) allows JavaScript inside JS runtimes to call external native shared libraries, to do things which would be otherwise difficult to do in JS. As of today, Porffor now has (early/experimental) support for it! It is currently limited to the native/C target in this early version. To test and benchmark, I adapted a benchmark by Divy Srivastava which uses sqlite3. Porffor’s dlopen API intentionally has ~identical arguments to Deno, only differing slightly with the return value, looking like:

const {
  sqlite3_initialize,
  sqlite3_open_v2,
  sqlite3_exec,
  sqlite3_prepare_v2,
  sqlite3_reset,
  sqlite3_step,
  sqlite3_column_int,
} = Porffor.dlopen("libsqlite3.so.0", {
  sqlite3_initialize: {
    parameters: [],
    result: "i32",
  },
  sqlite3_open_v2: {
    parameters: [
      "buffer", // const char *filename
      "buffer", // sqlite3 **ppDb
      "i32", // int flags
      "pointer", // const char *zVfs
    ],
    result: "i32",
  },
  // ...

…and the benchmark results are very promising! Around 10% faster than Deno, which has a lot of its own tech just to optimize FFI and is well regarded for its FFI performance. It is also ~30% faster than Bun, which has decent FFI performance. Node does not really have built-in FFI so is not compared here, but it would likely be slower than Bun.

The benchmark measures FFI performance by running SQL queries in sqlite3, as there are a lot of FFI calls and not much JS work (for integers), so it demos FFI overhead nicely while being more realistic by using a popular library, in my findings and opinion.

Thanks for reading! You can follow progress and message or email me in these places :^)