Performance and Comparison
Why WASM for Hydra SDK?
Hydra SDK leverages WebAssembly (WASM) to overcome the limitations of traditional JavaScript-based SDKs like @cardano-sdk. Here’s why WASM is the optimal choice:
Key Advantages of WASM
- Eliminates Node.js Core Module Dependencies:
- WASM allows critical functionalities (e.g., cryptography, CBOR parsing, transaction building) to run in a sandboxed environment without relying on Node.js core modules like
crypto,stream, orfs. - This ensures compatibility across both browser and Node.js environments.
- WASM allows critical functionalities (e.g., cryptography, CBOR parsing, transaction building) to run in a sandboxed environment without relying on Node.js core modules like
- Reduces Bundle Size:
- WASM loads only the required functions, avoiding the need for polyfills like
crypto-browserifyorstream-browserify. - This results in significantly smaller bundles compared to SDKs that rely on Node.js modules.
- WASM loads only the required functions, avoiding the need for polyfills like
- Boosts Performance:
- WASM executes cryptographic operations and transaction parsing at near-native speeds, reducing the overhead of JavaScript runtime.
- Cross-Platform Compatibility:
- A single WASM binary can run seamlessly on browsers, Node.js (via WASI), and mobile platforms (via bridges).
- This eliminates the need for complex interop between CommonJS and ESM modules.
Comparison of Cardano SDK Solutions
Detailed Comparison Table
| Criteria | SDKs based on @cardano-sdk | Hydra SDK (WASM-based) |
|---|---|---|
| Browser Compatibility | Requires polyfills for Node.js core modules; some APIs (e.g., fs, net) are unavailable. | Native browser support; no polyfills needed. |
| Bundle Size | Very large (hundreds of KB to >1MB) due to polyfills. | Compact (only loads WASM + minimal JS glue code). |
| Node.js Dependency | High, with direct imports of Node.js core modules. | None; all core logic resides in WASM. |
| Interop (ESM/CJS) | Prone to errors (exports is not defined, .default is not a function). | No issues; pure ESM JavaScript. |
| Crypto/CBOR Performance | Moderate (pure JS, dependent on low-performance polyfills). | High (WASM near-native speed). |
| Cross-Platform Support | Good for Node.js, limited for browsers. | Excellent; works on browsers, Node.js, and mobile. |
| Tree-Shaking | Limited, as Node.js imports are scattered, leading to unused code in bundles. | Excellent; only required WASM functions are bundled. |
| Maintenance & Scalability | Complex, as it requires tracking Node.js API changes and polyfills. | Easy to maintain; core logic is implemented in Rust/C++ and exposed via WASM. |
Dependency Flow Diagrams
Issues with SDKs based on @cardano-sdk
mermaid
graph TD
A[App Browser] --> B[SDKs based on @cardano-sdk]
B --> C[Node Core Modules]
C -->|Requires Polyfill| D[crypto-browserify, stream-browserify, buffer, util...]
D -->|Increases Bundle Size| E[Runtime Errors without Polyfill]
C -->|Cannot Polyfill| F[fs, net -> Unusable Features]
Hydra SDK (WASM-based)
mermaid
graph TD
A[App Browser] --> B[Hydra SDK]
B --> C["WASM Core (Rust/C++)"]
C --> D[WebAssembly Runtime]
D -->|Native Browser API| E[Crypto, CBOR, Tx Builder]
D -->|Node.js WASI| F[Crypto, CBOR, Tx Builder]
Conclusion
Hydra SDK’s WASM-based architecture provides a robust, efficient, and cross-platform solution for modern Cardano DApps. By eliminating Node.js dependencies, reducing bundle size, and boosting performance, it addresses the limitations of SDKs based on @cardano-sdk.
