Viem vs. Ethers.js: A Detailed Comparison for Web3 Developers

This blog explores the differences, benefits, and trade-offs between Ethers.js and Viem for building dapps on Ethereum

by Sushmita RashidMay 16, 2024
Viem vs. Ethers feature image

When building dapps on Ethereum, choosing the right library can significantly impact your development process and project success. Two prominent libraries in this space are Ethers.js and Viem. Both offer a powerful set of tools for connecting to smart contracts, sending transactions, and querying blockchain data with official support for TypeScript. Both libraries also adopt a modular design, allowing for the selective inclusion of library parts to optimize bundle size and application performance.

In this blog post, we'll explore these libraries in detail, highlighting their key differences, benefits, and trade-offs, as well as when to use one over the other. In the end, you should have all the information you need to decide when starting your next project.

Venn Diagram ethers vs viem

The key difference between Ethers.js and Viem


While they share a common goal, Ethers.js and Viem differ significantly in their approach and features, catering to varied developer preferences and project requirements.

Ethers.js


  • Providers in Ethers.js: Ethers.js uses providers as a bridge to the Ethereum network. These providers are versatile objects through which developers can read blockchain data or send transactions. Ethers.js simplifies the connection process to various Ethereum nodes via multiple provider types, including default, JSON-RPC, and Infura providers. This abstraction allows developers to concentrate more on their dapp's functionality rather than on the complexities of network connection.
  • Maturity and Stability: Ethers.js offers a long history and a robust support community, making it a reliable choice for complex projects. With a comprehensive feature set, it provides a wide range of functionalities, ideal for developers seeking a one-stop solution for Ethereum interaction. Despite its comprehensive nature, Ethers.js maintains a modular design, allowing developers to pick and choose specific functionalities.

Viem


  • Actions with Viem: Viem introduces the concept of actions — functions dedicated to executing specific tasks on the Ethereum network. These can be categorized into public actions (like retrieving a transaction) and wallet actions (such as sending a transaction). Designed for composability and modularity, Viem's actions enable developers to craft Ethereum applications with greater clarity and efficiency.
  • Efficiency and Minimalism: Viem focuses on a minimalist design, optimizing for efficiency with a smaller bundle size and selective execution of tasks. It emphasizes modularity and composability through its APIs, enabling a leaner, more efficient development process. With an innovative approach, Viem aims for a straightforward API design, prioritizing clarity and ease of use to enhance the developer experience.

Choosing between Ethers.js and Viem


Choosing between Ethers.js and Viem depends on the specific needs of your project and your preferences as a developer.

  • Ethers.js is best suited for projects that demand a comprehensive set of features and prioritize maturity and community support. It's ideal for complex dapps requiring extensive interaction with the Ethereum network.
  • Viem is the go-to choice for projects where efficiency, a lightweight bundle, and a minimalist design are key considerations. It shines in web applications focused on speed and performance, as well as for developers who prefer a modular and innovative approach to Ethereum interaction.

Benefits and Tradeoffs of Ethers.js and Viem


Ethers.js


Benefits:

  • Broad range of features and strong community support.
  • High stability and reliability derived from its maturity.

Tradeoffs:

  • Potentially overwhelming for projects that do not require its extensive feature set.
  • Managing dependencies and external providers can add complexity.

Viem


Benefits:

  • Efficient and lightweight, perfect for performance-sensitive applications.
  • Simplifies the development process with its modular design and clear documentation.

Tradeoffs:

  • Less mature than Ethers.js, with a smaller community and fewer resources.
  • May require more effort to manage due to its unopinionated, modular nature.

Technical differences between Ethers.js and Viem


Below is an enhanced comparison table between Ethers.js and Viem, featuring code examples and brief explanations for various functionalities. This comparison highlights the structural and conceptual differences in how each library approaches interactions with the Ethereum blockchain.

FULL CHART

Other things to keep in mind


Terminology and Conceptual Clarity: The term "provider" is widely used in web3 development but can often lead to confusion due to its overloaded meaning. Viem addresses this by employing the term "Client," categorizing them into Public Client (for blockchain information access), Wallet Client (for transaction signing), and Test Client (for local node testing). This distinction helps developers better navigate the Ethereum interaction landscape.

Bundle Size and Efficiency: Viem is designed with a minimalist modular architecture, boasting a bundle size of only 35kB. This contrasts sharply with Ethers.js, which, due to its broader range of built-in functionalities, results in a larger bundle size and potentially impacts web application performance more significantly.

API Design and Development Experience: Viem opts for verbose APIs, prioritizing flexibility and ease of change. This approach stands in contrast to Ethers.js, which may employ more abstracted APIs for some functionalities, potentially making it harder to modify or extend the codebase. Additionally, leveraging TypeScript, Viem provides strongly typed APIs, enhancing the development experience through features like autocomplete, type inference, and static validation. This reduces the chance of runtime errors and makes the API more intuitive.

Conclusion


Each library offers a distinct approach to interacting with the Ethereum blockchain. Ethers.js tends to abstract complexities through predefined classes and methods, aiming for an object-oriented experience. In contrast, Viem opts for a more function-driven interface, emphasizing explicitness and perhaps offering a slightly steeper learning curve but greater flexibility in certain contexts.

Ethers.js is geared towards developers seeking a comprehensive toolkit for complex dApps, offering a rich set of abstractions and functionalities. On the other hand, Viem caters to those who value efficiency, modularity, and a minimalist design, providing a streamlined and innovative approach to Ethereum development. The choice between the two ultimately rests on the developer's or project's specific requirements and preferences.

Receive our Newsletter