Say WAT Now!? Turbocharged JavaScript With Hand Crafted WASM

Rate this content
Bookmark

Breathe a sigh of relief — you don’t need to learn Rust to use WASM! WebAssembly has its own human readable language: Web Assembly Text format (WAT), and it’s surprisingly easy to use. In this talk we’ll discuss using WAT as a first-class language for enhancing JavaScript performance.

This talk has been presented at JSNation 2024, check out the latest edition of this JavaScript Conference.

FAQ

WebAssembly, or Wasm, is a virtual stack machine specification that is hardware independent. It allows you to build applications once and run them anywhere, similar to the promise of JavaScript. Wasm can be run in the browser, in Node.js, and as a system utility.

JavaScript's success can be attributed to its ease of learning and its unique position as the only runtime in browsers for a long time. This made it essential for web development.

WebAssembly offers several benefits including hardware independence, improved performance for CPU-intensive tasks, and the ability to run code written in other languages like Rust, C, and C++ in the browser.

WebAssembly can be used in JavaScript projects to accelerate performance, especially for CPU-intensive tasks like canvas rendering and game optimizations. Developers can write small amounts of handwritten Wasm and run it directly from JavaScript code.

The WebAssembly text format (WAT) is a human-readable representation of WebAssembly binary code. It allows developers to write and understand WebAssembly code more easily by providing readable labels and syntax.

One useful tool is Wabbit (or Rabbit), the WebAssembly binary toolkit. It allows you to convert between binary and WebAssembly text format (WAT), making it easier to read and write WebAssembly code.

Yes, WebAssembly can be run outside of browsers. It can be executed in Node.js and as a system utility, making it versatile for various applications.

Languages like Rust, C, C++, and Go can be compiled into WebAssembly, allowing developers to run code written in these languages in the browser or other environments that support Wasm.

WebAssembly improves performance by allowing developers to write code that runs closer to the hardware level, making it suitable for CPU-intensive tasks. This can significantly speed up performance for specific parts of an application.

The handcrafted Wasm project by Austin Theroux demonstrates the potential of handwritten WebAssembly code to achieve high performance in tasks like canvas rendering. His projects, such as Conway's Game of Life, show how Wasm can be used to optimize performance.

Justin Schroeder
Justin Schroeder
11 min
17 Jun, 2024

Comments

Sign in or register to post your comment.

Video Summary and Transcription

JavaScript became successful due to its association with the browser as the only runtime. WebAssembly (WASM) is a virtual machine that can run anywhere and has a different computation model. WebAssembly code can be analyzed and converted into a more readable format. It can be used to accelerate code and enhance performance. Check out Austin Theroux's repository for examples.

1. Introduction to JavaScript and WebAssembly

Short description:

JavaScript became successful due to its association with the browser as the only runtime. However, WebAssembly (WASM) has been available as a runtime in the browser since 2017. Let's explore the benefits and what WebAssembly is.

Hey, and welcome to my talk, Say What Now? Turbocharged JavaScript with Handcrafted Wasm. Let's get into it.

Take a look at this chart. Starting on the left, we've got Lua, Dart, Ruby, working all the way to the right of all these different programming languages. And on the far right side, by far the most programmers in the world are JavaScript programmers. Have you ever wondered why JavaScript was so successful?

Okay, before I answer that, real quick, my intro. I'm Justin. You can follow me on X, and I consider myself an open-source creator. Some projects that I've worked on are FormKit, which is a form-building framework in the Vue ecosystem. Arrow.js is a small, lightweight, reactive framework. AutoAnimate does automatic animations on your applications. Tempo is a datetime library, sort of like Moment.js. Drag and drop to make your drag and drops easy. And Vue.Formulate for the old days of Vue 2. But I'm going to get back into that question, why was JavaScript so successful? And I think that this is a really interesting question that has a few different variables playing into it. Certainly, if you look to the right-hand side of this list of developers, you have some of the easier languages to learn. Maybe not to master, but to learn. Things like JavaScript, but also Python is famously user-friendly. But I think the real reason why JavaScript took it is because of the browser. In fact, JavaScript is the only runtime in the browser, and has been for a super long time. Although I'm sure many of you know this, that's not entirely true anymore. Starting in 2017, which if you look at a calendar, is actually a long time ago. It's like a full five years ago now, more than that, six years ago now. WASM, or WebAssembly, has been also available as a runtime in the browser. And yet, we don't seem to use that much of it. What are its benefits? What's it all about? That's what I want to talk to you about. So first of all, what is WebAssembly? It's a virtual stack machine specification. Let me dig into this just a little bit more. So virtual, it's a virtual machine. That basically just means it's hardware independent.

2. Overview of WebAssembly

Short description:

JavaScript allows code to be written once and run anywhere, just like Java. WebAssembly (Wasm) is a virtual machine that can also run anywhere. It uses a stack machine computation model, which is different from most assembly languages. Wasm is defined by a specification that can be implemented by different runtimes, allowing it to be utilized in various environments. It is a binary format that can be translated into hexadecimal for more detailed analysis.

You build it once and you run it everywhere, right? That was always the promise of Java back in the day. And frankly, it's one of the promises of JavaScript. You write your JavaScript and you can run it in the browser, you can run it in Node, you can run it all over the place. You don't need to worry about, you know, hey, is this a 64-bit or a 32-bit CPU? Is it running some particular chipset that doesn't work with the way that I compile these binaries? It's not like that. And Wasm is not like that either. It is a virtual machine that you can run anywhere.

Now, how about this one, stack machine? That's probably a little bit less familiar to people. And we're going to get into this a little bit more. But essentially, this is the computation model. Every machine that does computation needs a way to do that computation. You know, the sort of maybe the most famous one might be a Turing machine. But there are other computation models, like a register machine, which is what most assemblies are written in. This one's a little bit different. It's a stack machine. We're going to look into that.

And then finally, it's a specification. And that's actually maybe the most interesting part, is there's no canonical implementation of Wasm. Instead, it's a specification that anybody can comply with and have done so. There's browser runtimes, but there's also runtimes, you know, you can run Wasm in Node.js. And now you have systems level runtime. So you could compile your Rust binary, not just to run in the browser, but also to run as a system utility running in Wasm. So you don't have to compile to all the different binary formats. All right. What does it actually look like? Well, fundamentally, this is a binary format. So here's what it looks like. It looks like a bunch of binary code. But if we were to take this, this is an actual application you're looking at here. And in Wasm, all bits are 8 bits, or all bytes are 8 bits. So we could translate this into hex, for example. And we could see a little bit more resolution.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

A Guide to React Rendering Behavior
React Advanced Conference 2022React Advanced Conference 2022
25 min
A Guide to React Rendering Behavior
Top Content
This transcription provides a brief guide to React rendering behavior. It explains the process of rendering, comparing new and old elements, and the importance of pure rendering without side effects. It also covers topics such as batching and double rendering, optimizing rendering and using context and Redux in React. Overall, it offers valuable insights for developers looking to understand and optimize React rendering.
Speeding Up Your React App With Less JavaScript
React Summit 2023React Summit 2023
32 min
Speeding Up Your React App With Less JavaScript
Top Content
Watch video: Speeding Up Your React App With Less JavaScript
Mishko, the creator of Angular and AngularJS, discusses the challenges of website performance and JavaScript hydration. He explains the differences between client-side and server-side rendering and introduces Quik as a solution for efficient component hydration. Mishko demonstrates examples of state management and intercommunication using Quik. He highlights the performance benefits of using Quik with React and emphasizes the importance of reducing JavaScript size for better performance. Finally, he mentions the use of QUIC in both MPA and SPA applications for improved startup performance.
Utilising Rust from Vue with WebAssembly
Vue.js London Live 2021Vue.js London Live 2021
8 min
Utilising Rust from Vue with WebAssembly
Top Content
In this Talk, the speaker demonstrates how to use Rust with WebAssembly in a Vue.js project. They explain that WebAssembly is a binary format that allows for high-performance code and less memory usage in the browser. The speaker shows how to build a Rust example using the WasmPack tool and integrate it into a Vue template. They also demonstrate how to call Rust code from a Vue component and deploy the resulting package to npm for easy sharing and consumption.
React Concurrency, Explained
React Summit 2023React Summit 2023
23 min
React Concurrency, Explained
Top Content
Watch video: React Concurrency, Explained
React 18's concurrent rendering, specifically the useTransition hook, optimizes app performance by allowing non-urgent updates to be processed without freezing the UI. However, there are drawbacks such as longer processing time for non-urgent updates and increased CPU usage. The useTransition hook works similarly to throttling or bouncing, making it useful for addressing performance issues caused by multiple small components. Libraries like React Query may require the use of alternative APIs to handle urgent and non-urgent updates effectively.
The Future of Performance Tooling
JSNation 2022JSNation 2022
21 min
The Future of Performance Tooling
Top Content
Today's Talk discusses the future of performance tooling, focusing on user-centric, actionable, and contextual approaches. The introduction highlights Adi Osmani's expertise in performance tools and his passion for DevTools features. The Talk explores the integration of user flows into DevTools and Lighthouse, enabling performance measurement and optimization. It also showcases the import/export feature for user flows and the collaboration potential with Lighthouse. The Talk further delves into the use of flows with other tools like web page test and Cypress, offering cross-browser testing capabilities. The actionable aspect emphasizes the importance of metrics like Interaction to Next Paint and Total Blocking Time, as well as the improvements in Lighthouse and performance debugging tools. Lastly, the Talk emphasizes the iterative nature of performance improvement and the user-centric, actionable, and contextual future of performance tooling.
Making JavaScript on WebAssembly Fast
JSNation Live 2021JSNation Live 2021
29 min
Making JavaScript on WebAssembly Fast
Top Content
WebAssembly enables optimizing JavaScript performance for different environments by deploying the JavaScript engine as a portable WebAssembly module. By making JavaScript on WebAssembly fast, instances can be created for each request, reducing latency and security risks. Initialization and runtime phases can be improved with tools like Wiser and snapshotting, resulting in faster startup times. Optimizing JavaScript performance in WebAssembly can be achieved through techniques like ahead-of-time compilation and inline caching. WebAssembly usage is growing outside the web, offering benefits like isolation and portability. Build sizes and snapshotting in WebAssembly depend on the application, and more information can be found on the Mozilla Hacks website and Bike Reliance site.

Workshops on related topic

React Performance Debugging Masterclass
React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Top Content
Featured WorkshopFree
Ivan Akulov
Ivan Akulov
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)
Building WebApps That Light Up the Internet with QwikCity
JSNation 2023JSNation 2023
170 min
Building WebApps That Light Up the Internet with QwikCity
Featured WorkshopFree
Miško Hevery
Miško Hevery
Building instant-on web applications at scale have been elusive. Real-world sites need tracking, analytics, and complex user interfaces and interactions. We always start with the best intentions but end up with a less-than-ideal site.
QwikCity is a new meta-framework that allows you to build large-scale applications with constant startup-up performance. We will look at how to build a QwikCity application and what makes it unique. The workshop will show you how to set up a QwikCitp project. How routing works with layout. The demo application will fetch data and present it to the user in an editable form. And finally, how one can use authentication. All of the basic parts for any large-scale applications.
Along the way, we will also look at what makes Qwik unique, and how resumability enables constant startup performance no matter the application complexity.
Next.js 13: Data Fetching Strategies
React Day Berlin 2022React Day Berlin 2022
53 min
Next.js 13: Data Fetching Strategies
Top Content
WorkshopFree
Alice De Mauro
Alice De Mauro
- Introduction- Prerequisites for the workshop- Fetching strategies: fundamentals- Fetching strategies – hands-on: fetch API, cache (static VS dynamic), revalidate, suspense (parallel data fetching)- Test your build and serve it on Vercel- Future: Server components VS Client components- Workshop easter egg (unrelated to the topic, calling out accessibility)- Wrapping up
React Performance Debugging
React Advanced Conference 2023React Advanced Conference 2023
148 min
React Performance Debugging
Workshop
Ivan Akulov
Ivan Akulov
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)
High-performance Next.js
React Summit 2022React Summit 2022
50 min
High-performance Next.js
Workshop
Michele Riva
Michele Riva
Next.js is a compelling framework that makes many tasks effortless by providing many out-of-the-box solutions. But as soon as our app needs to scale, it is essential to maintain high performance without compromising maintenance and server costs. In this workshop, we will see how to analyze Next.js performances, resources usage, how to scale it, and how to make the right decisions while writing the application architecture.
Maximize App Performance by Optimizing Web Fonts
Vue.js London 2023Vue.js London 2023
49 min
Maximize App Performance by Optimizing Web Fonts
WorkshopFree
Lazar Nikolov
Lazar Nikolov
You've just landed on a web page and you try to click a certain element, but just before you do, an ad loads on top of it and you end up clicking that thing instead.
That…that’s a layout shift. Everyone, developers and users alike, know that layout shifts are bad. And the later they happen, the more disruptive they are to users. In this workshop we're going to look into how web fonts cause layout shifts and explore a few strategies of loading web fonts without causing big layout shifts.
Table of Contents:What’s CLS and how it’s calculated?How fonts can cause CLS?Font loading strategies for minimizing CLSRecap and conclusion