Performance of cardano-serialization-lib-asmjs

I’m wondering if anyone else is experience slow performance with the ASM.js version of cardano-serialization-lib. Encryption-related operations such as signing a transaction take 15+ seconds and 100% utilization of a CPU core.

Also, can anyone point me to an example of using the WASM version of this library from JavaScript? (It seems to have a circular dependency that prevents it from being imported.) I’m hoping that the WASM version will be much faster than the ASM.js version.

I’m looking for the same thing I cant get WASM version to import, i think we could use webpack to do it. If you already manage to do it could you share your approach?

I haven’t investigated this further—I’m concerned about investing a lot time in getting the WASM working without knowing whether it will be much faster than the ASM.js. (I’m just using this for a simple experimental DApp for my son to request and receive reimbursement from me for incidental expenses, so slow performance on signing transactions isn’t a big deal.)

Even though Rust’s wasm-bindgen tool advertises that it lets JavaScript call Rust code like the Cardano serialization library, I haven’t found online examples that I could model my application after. I think the three best alternative approaches would be (1) carefully work though a simple wasm-bindgen example to figure out the proper way to import (either with or without webpack) a wasm-bindgen library, and then use that technique to import the serialization library; (2) write the whole application in Rust instead of JavaScript; or (3) look for another JavaScript library that provides the ability to build and sign transactions. I’ll post here if I pursue option 1 and find a solution.

2 Likes

There’s the following, linked from CIP-0008:

https://repl.it/repls/FlusteredSimpleFunction

(it forces you to signup for an account first, before you can view the sample code)

I got the nodejs version working as expected. But I haven’t tried the browser version yet.

2 Likes

Thanks so much for the information and the link!

FYI, I also found these instructions for using Rust WASM without nodejs, but I haven’t tried them yet. Notably, the instructions include a method for shrinking the .wasm file: this might be useful since the serialization library WASM is about 1.3 MB.

1 Like

wow! From 822 Kb, down to only 16Kb!

I’m new to Rust and WASM but this is good info… thanks! Will revisit this when I get to that point in my project where I need this.

Here is a recipe for using the WASM version of cardano-serialization-lib without nodejs. The WASM version is immensely faster than the ASM.js version.

  1. Clone the cardano-serialization-lib repository.

  2. Instead of using the npm build, just do a Rust build within the rust/ folder:

  3. Make sure that Rust is installed. On NixOS, one can use the following:

    nix-shell -p rustup pkg-config openssl.devel
    
  4. Install wasm-pack:

    cargo install wasm-pack
    
  5. Use the --target=web procedure in the WASM w/o a Bundler instructions. Here is the command:

    wasm-pack build --target=web --no-typescript --release
    
  6. The pkg/ folder will contain three artifacts, and the last two will be used in the browser application:

    • package.json
    • cardano_serialization_lib.js
    • cardano_serialization_lib_bg.wasm
  7. In application’s JavaScript startup code, import and initialize the library:

    import init, * as CSL from "./lib/cardano_serialization_lib.js"
    
    init().then(() => {
      // other startup code
    })
2 Likes