<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vo Van Thanh</title>
    <description>The latest articles on DEV Community by Vo Van Thanh (@jthanh8144).</description>
    <link>https://dev.to/jthanh8144</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1487608%2F614d1d4b-8243-44b5-b723-acc4381db7fd.png</url>
      <title>DEV Community: Vo Van Thanh</title>
      <link>https://dev.to/jthanh8144</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jthanh8144"/>
    <language>en</language>
    <item>
      <title>Frameworks of BunJS: ElysiaJS and Hono</title>
      <dc:creator>Vo Van Thanh</dc:creator>
      <pubDate>Wed, 15 May 2024 01:44:52 +0000</pubDate>
      <link>https://dev.to/jthanh8144/frameworks-of-bunjs-elysiajs-and-hono-4all</link>
      <guid>https://dev.to/jthanh8144/frameworks-of-bunjs-elysiajs-and-hono-4all</guid>
      <description>&lt;p&gt;BunJS is hot, but what about frameworks?  This post dives into ElysiaJS and Hono, exploring how they can take your BunJS development to the next level.&lt;/p&gt;




&lt;h2&gt;
  
  
  What's BunJS
&lt;/h2&gt;

&lt;p&gt;BunJS is a fast and versatile JavaScript runtime designed to be a drop-in replacement for Node.js. Similar to Node.js and Deno, it allows you to execute JavaScript code outside of a web browser. However, BunJS stands out for its focus on performance.&lt;/p&gt;

&lt;p&gt;BunJS was built from scratch to focus on four key points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Speed: BunJS utilizes the latest JavaScriptCore engine and is written in Zig, a language known for its efficiency. This combination contributes to BunJS's exceptional performance compared to Node.js and Deno used V8 engine.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;All-in-one Toolkit: BunJS goes beyond just a runtime. It provides a built-in bundler, test runner, and package manager, streamlining the development workflow.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Node.js Compatibility: BunJS natively implements many Node.js APIs and Web APIs, making it easier to migrate existing Node.js projects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;TypeScript Support: BunJS offers seamless integration with TypeScript, allowing developers to write robust and maintainable code.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;According to the official site&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The goal of Bun is to run most of the world's server-side JavaScript and provide tools to improve performance, reduce complexity, and multiply developer productivity.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In summary, BunJS is a promising newcomer in the JavaScript runtime landscape, offering exceptional speed, a comprehensive toolkit, and compatibility with existing Node.js code. It's worth considering for projects that demand high performance and a smooth development experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compare speed of NodeJS and BunJS
&lt;/h2&gt;

&lt;p&gt;I found the performance benchmarking of 5 runtime: Bun, NodeJS, Go, C# and Python. You can read full post &lt;a href="https://www.wwt.com/blog/performance-benchmarking-bun-vs-c-vs-go-vs-nodejs-vs-python" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdctw351icypm9gnkm0g6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdctw351icypm9gnkm0g6.png" alt="Compare speed of NodeJS, Bun, C# and Python" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The result of the benchmarking according to requests per second shows that: in the beta version, Bun is almost twice as fast as NodeJS (with Fastify framework).&lt;/p&gt;

&lt;p&gt;Next, we will explore the prominent frameworks currently available for BunJS.&lt;/p&gt;




&lt;h2&gt;
  
  
  ElysiaJS
&lt;/h2&gt;

&lt;p&gt;Elysia is a HTTP API framework written on Bun with developer experience being the main motivation of the framework. Essentially, it’s a faster Express.js replacement.&lt;br&gt;
You can create new Elysia project using command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bun create elysia &amp;lt;name of the project&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A basic Elysia endpoint could look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5rw02zkt37zk8102m2jd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5rw02zkt37zk8102m2jd.png" alt="Elysia" width="800" height="297"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Routing
&lt;/h3&gt;

&lt;p&gt;We can define a route by calling a method named after HTTP verbs, passing a path and a function to execute when matched.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5l801i8zh3h33q811lti.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5l801i8zh3h33q811lti.png" alt="Elysia routing" width="800" height="209"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Elysia is using method chaining to synchronize type safety for later use. So, always use method chaining in Elysia.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk49bf5y44aaw0dz83g86.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk49bf5y44aaw0dz83g86.png" alt="Elysia method chaning" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Addition, Elysia accept custom HTTP Methods with &lt;code&gt;Elysia.route&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fquhticfzcwrpbt82r7mr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fquhticfzcwrpbt82r7mr.png" alt="Elysia custom method" width="800" height="234"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Path
&lt;/h3&gt;

&lt;p&gt;The path in Elysia can be grouped into 3 types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Static paths - static string to locate the resource&lt;/li&gt;
&lt;li&gt;Dynamic paths - segment can be any value&lt;/li&gt;
&lt;li&gt;Wildcards - path until a specific point can be anything&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The priorities are as follows: static &amp;gt; dynamic &amp;gt; wildcards&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fac6mlp2uuhwu2ppokern.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fac6mlp2uuhwu2ppokern.png" alt="Elysia path" width="800" height="238"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Handler &amp;amp; Context
&lt;/h3&gt;

&lt;p&gt;After a resource is located, a function that respond is refers as handler&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ss2dadw5gu7dmp8p973.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ss2dadw5gu7dmp8p973.png" alt="Elysia handler" width="800" height="212"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Context is a request information passed to a route handler. Context is unique for each request, and is not shared except for store which is a global mutable state. Elysia context includes path, body, query, params, headers, request, redirect, store, cookie, set and error. You can see more information about it in &lt;a href="https://elysiajs.com/essential/context.html" rel="noopener noreferrer"&gt;the official documents&lt;/a&gt; of Elysia.&lt;/p&gt;

&lt;h3&gt;
  
  
  Life cycle event
&lt;/h3&gt;

&lt;p&gt;Elysia's Life Cycle event can be illustrated as the following&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flrupx127zvjxcb59unky.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flrupx127zvjxcb59unky.png" alt="Elysia life cycle" width="800" height="492"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Plugin
&lt;/h3&gt;

&lt;p&gt;The plugin pattern is a powerful approach to manage this complexity. It promotes modularity, which means breaking down functionality into smaller, reusable pieces.&lt;/p&gt;

&lt;p&gt;We can use the plugin by passing an instance to &lt;code&gt;Elysia.use&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh4ope0w2npwl93qynm05.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh4ope0w2npwl93qynm05.png" alt="Elysia plugin" width="800" height="324"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The plugin will inherit all properties of the plugin instance, including state, decorate, derive, route, lifecycle....&lt;/p&gt;

&lt;p&gt;Elysia will also handle the type inference automatically as well, so you can imagine as if you call all of the other instances on the main one.&lt;/p&gt;

&lt;p&gt;You can find an officially maintained plugin at &lt;a href="https://elysiajs.com/plugins/overview.html" rel="noopener noreferrer"&gt;Elysia's plugins&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have created a boilerplate for Elysia. You can find it &lt;a href="https://github.com/jthanh8144/elysia-boilerplate" rel="noopener noreferrer"&gt;here&lt;/a&gt;. It's based on three repositories, thanks to its authors.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/elysiajs/awesome-elysia?tab=readme-ov-file" rel="noopener noreferrer"&gt;https://github.com/elysiajs/awesome-elysia?tab=readme-ov-file&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.dev/OthmanAmoudi/Elysiajs-api-controllers" rel="noopener noreferrer"&gt;https://github.dev/OthmanAmoudi/Elysiajs-api-controllers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.dev/Leka-Workshop/Bun-CRUD-App" rel="noopener noreferrer"&gt;https://github.dev/Leka-Workshop/Bun-CRUD-App&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Hono
&lt;/h2&gt;

&lt;p&gt;Hono is a lightweight, simple, and fast web framework for Cloudflare Workers, Deno, Bun, and other applications. It is a modern web application that is both fast and flexible. It offers inbuilt support for TypeScript, and easy development in a local environment.&lt;/p&gt;

&lt;p&gt;Start Hono project with command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bun create hono &amp;lt;name of the project&amp;gt;
cd &amp;lt;name of the project&amp;gt;
bun install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The basic project same as&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgswtvish2w9ly73z2zjf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgswtvish2w9ly73z2zjf.png" alt="Hono basic" width="800" height="207"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Routing
&lt;/h3&gt;

&lt;p&gt;Hono provides two main routing mechanisms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TrieRouter: This is the default and recommended router. It utilizes a trie data structure for efficient pattern matching, leading to faster performance, especially for applications with a large number of routes.&lt;/li&gt;
&lt;li&gt;RegExpRouter: it is the fastest router in the JavaScript world. They are using linear loops. Therefore, regular expression matching will be performed for all routes and the performance will be degraded as you have more routes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Furthermore, Hono also has three other types of routers: SmartRouter, LinearRouter, and PatternRouter.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5kj4qngfiq5xxbvf5iey.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5kj4qngfiq5xxbvf5iey.png" alt="Hono routing" width="800" height="648"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Context
&lt;/h3&gt;

&lt;p&gt;To handle Request and Response, you can use &lt;code&gt;Context&lt;/code&gt; object.&lt;br&gt;
It includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;req&lt;/li&gt;
&lt;li&gt;body()&lt;/li&gt;
&lt;li&gt;text()&lt;/li&gt;
&lt;li&gt;json()&lt;/li&gt;
&lt;li&gt;html()&lt;/li&gt;
&lt;li&gt;notFound()&lt;/li&gt;
&lt;li&gt;redirect()&lt;/li&gt;
&lt;li&gt;res&lt;/li&gt;
&lt;li&gt;etc…&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can read more information about it in the official document of Hono.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exception
&lt;/h3&gt;

&lt;p&gt;When a fatal error occurs, such as authentication failure, an HTTPException must be thrown.&lt;/p&gt;

&lt;p&gt;This example throws an HTTPException from the middleware.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8tommc6ma51dnc5jnhv4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8tommc6ma51dnc5jnhv4.png" alt="Hono throw exception" width="800" height="342"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can handle the thrown HTTPException with &lt;code&gt;app.onError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fprxcntz45mfbj3u9mwim.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fprxcntz45mfbj3u9mwim.png" alt="Hono handle exception" width="800" height="335"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Middleware
&lt;/h3&gt;

&lt;p&gt;Middleware works after/before Handler. We can get Request before dispatching or manipulate Response after dispatching.&lt;/p&gt;

&lt;p&gt;Definition of Middleware:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Handler - should return Response object. Only one handler will be called.&lt;/li&gt;
&lt;li&gt;Middleware - should return nothing, will be proceeded to next middleware with await next()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The user can register middleware using &lt;code&gt;app.use&lt;/code&gt; or using &lt;code&gt;app.HTTP_METHOD&lt;/code&gt; as well as the handlers. For this feature, it's easy to specify the path and the method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fybkm4e1dny4tqlkomzqo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fybkm4e1dny4tqlkomzqo.png" alt="Hono middleware" width="800" height="259"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Execution order&lt;/strong&gt;: The order in which Middleware is executed is determined by the order in which it is registered. The process before the next of the first registered Middleware is executed first, and the process after the next is executed last. See below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3z6o9ichozocved70nzc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3z6o9ichozocved70nzc.png" alt="Hono middleware execution order" width="800" height="595"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Result is the following&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F52fhn25w02aqpndhgq92.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F52fhn25w02aqpndhgq92.png" alt="Hono middleware execution order result" width="800" height="232"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hono has some built-in middlewares such as logger or basicAuth. You can write your own middleware directly inside &lt;code&gt;app.use()&lt;/code&gt;. Besides, Hono also has some third-party middlewares of GraphQL Server, Sentry, Firebase Auth… (You can see more &lt;a href="https://hono.dev/middleware/third-party" rel="noopener noreferrer"&gt;here&lt;/a&gt;) So with them, we may make a more complex application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Client components
&lt;/h3&gt;

&lt;p&gt;In Hono, you can write HTML with JSX syntax with &lt;code&gt;hono/jsx&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;On the server side, it supports some features of React such as Fragment, child props, optimizing components using &lt;code&gt;memo&lt;/code&gt;, context, etc.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnu4meyemcjd2ixjt3whr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnu4meyemcjd2ixjt3whr.png" alt="Hono client components" width="800" height="900"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;hono/jsx&lt;/code&gt; supports not only server side but also client side. It’s called Client Components or hono/jsx/dom. It has Hooks that are compatible or partially compatible with React: useState, useEffect, useRef, useCallback...&lt;/p&gt;

&lt;p&gt;Here is an example of a simple counter, the same code works as in React.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhnpvaf8iw4xpexyxqzpj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhnpvaf8iw4xpexyxqzpj.png" alt="Hono with react hook" width="800" height="735"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Boilerplate
&lt;/h3&gt;

&lt;p&gt;You can refer to my Hono API boilerplate &lt;a href="https://github.com/jthanh8144/hono-boilerplate" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Speed test ElysiaJS, Hono, Express, Fastify and NestJS (Express)
&lt;/h2&gt;

&lt;p&gt;Unit: req/s&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Framework&lt;/th&gt;
&lt;th&gt;Runtime&lt;/th&gt;
&lt;th&gt;Average&lt;/th&gt;
&lt;th&gt;Get(/)&lt;/th&gt;
&lt;th&gt;Params, query &amp;amp; header&lt;/th&gt;
&lt;th&gt;Post JSON&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ElysiaJS&lt;/td&gt;
&lt;td&gt;Bun&lt;/td&gt;
&lt;td&gt;275,063.507&lt;/td&gt;
&lt;td&gt;326,868.9&lt;/td&gt;
&lt;td&gt;261,729.3&lt;/td&gt;
&lt;td&gt;236,592.32&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hono&lt;/td&gt;
&lt;td&gt;Bun&lt;/td&gt;
&lt;td&gt;257,532.08&lt;/td&gt;
&lt;td&gt;320,757.07&lt;/td&gt;
&lt;td&gt;233,769.22&lt;/td&gt;
&lt;td&gt;218,069.95&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fastify&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;64,145.95&lt;/td&gt;
&lt;td&gt;74,631.46&lt;/td&gt;
&lt;td&gt;66,235.48&lt;/td&gt;
&lt;td&gt;51,570.91&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ExpressJS&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;16,301.823&lt;/td&gt;
&lt;td&gt;17,974.35&lt;/td&gt;
&lt;td&gt;17,090.62&lt;/td&gt;
&lt;td&gt;13,840.5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;NestJS (Express)&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;14,978.863&lt;/td&gt;
&lt;td&gt;16,926.01&lt;/td&gt;
&lt;td&gt;15,507.62&lt;/td&gt;
&lt;td&gt;12,502.96&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Reference&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/SaltyAom/bun-http-framework-benchmark/tree/c7e26fe3f1bfee7ffbd721dbade10ad72a0a14ab#results" rel="noopener noreferrer"&gt;https://github.com/SaltyAom/bun-http-framework-benchmark/tree/c7e26fe3f1bfee7ffbd721dbade10ad72a0a14ab#results&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;BunJS has emerged as a promising player in the JavaScript runtime landscape. Its focus on speed, efficiency, and a streamlined development experience makes it an attractive option for modern web development. In this post, we explored two exciting BunJS frameworks: ElysiaJS and Hono.&lt;/p&gt;

&lt;p&gt;ElysiaJS provides a comprehensive solution for building full-fledged web applications, offering features like routing, templating, and form handling. If you're looking for a rapid development framework to jumpstart your BunJS project, ElysiaJS is a strong contender.&lt;/p&gt;

&lt;p&gt;Hono, on the other hand, excels in lightweight routing and server-side functionality. Its flexibility and focus on performance make it ideal for building APIs and microservices. Whether you're building a complex web application or a performant API, BunJS, with frameworks like ElysiaJS and Hono, offers a powerful and versatile ecosystem for your next project.&lt;/p&gt;

&lt;p&gt;Consider diving deeper into these frameworks and exploring the possibilities BunJS unlocks. With its focus on speed and a growing ecosystem, BunJS is definitely worth keeping an eye on for the future of JavaScript development.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>bunjs</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
