Bringing Javascript to WebAssembly for Shopify Functions

While we’re working on getting our Shopify Functions infrastructure ready for the public beta, we thought we’d use this opportunity to shine some light on how we brought JavaScript to WebAssembly, how we made everything fit within our very tight Shopify Function constraints, and what our plans for the future look like.

Overview

The article discusses the integration of JavaScript into Shopify Functions via WebAssembly, highlighting the development of Javy, a toolchain for running JavaScript in a WebAssembly environment. It explores the constraints, architecture, and future plans for enhancing developer experience and performance within Shopify's ecosystem.

What You'll Learn

1

How to run JavaScript functions in a WebAssembly environment using Javy

2

Why WebAssembly is a suitable choice for executing untrusted code securely

3

When to use Shopify Functions for backend customization

Prerequisites & Requirements

  • Basic understanding of JavaScript and WebAssembly concepts
  • Familiarity with npm and command-line interfaces(optional)

Key Questions Answered

What are the constraints for Shopify Functions written in JavaScript?
Shopify Functions written in JavaScript must adhere to specific constraints: the module size must not exceed 256KB, execution time must be under 5ms, and it must consume and produce JSON-formatted strings via stdin and stdout. These limits ensure performance and scalability, especially during high-demand periods.
How does Javy enable JavaScript execution in WebAssembly?
Javy compiles a fast JavaScript interpreter, QuickJS, into WebAssembly, allowing JavaScript code to be executed within a Wasm module. This approach enables developers to write Shopify Functions in JavaScript while adhering to the performance and security constraints of the Shopify platform.
What is the future plan for JavaScript performance in Shopify Functions?
Shopify plans to enhance JavaScript performance in Functions by integrating Mozilla's SpiderMonkey into WebAssembly, which is expected to provide significant performance improvements over QuickJS. This initiative aims to support complex use cases while maintaining the tight performance constraints of the platform.

Key Statistics & Figures

Maximum module size for Shopify Functions
256KB
This size constraint is critical for ensuring quick execution and scalability during peak times.
Maximum execution time for Shopify Functions
5ms
This performance limit is essential for maintaining responsiveness in the Shopify ecosystem.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Programming Language
Javascript
Used for writing Shopify Functions within the WebAssembly environment.
Technology
Webassembly
Provides a secure and performant execution environment for JavaScript code.
Javascript Engine
Quickjs
Serves as the interpreter for executing JavaScript within the Javy toolchain.
Interface
Wasi
Enables WebAssembly modules to interact with the host environment, allowing for input and output operations.

Key Actionable Insights

1
Developers should experiment with the local developer preview of JavaScript in Shopify Functions to familiarize themselves with the new capabilities and constraints.
This hands-on experience will help developers understand how to optimize their functions for performance and scalability, preparing them for future deployment when the public beta is available.
2
Utilizing the Javy toolchain can streamline the process of writing and deploying JavaScript functions on Shopify.
By leveraging Javy, developers can focus on their business logic without worrying about the underlying WebAssembly complexities, thus enhancing productivity.

Common Pitfalls

1
Developers may overlook the performance constraints when writing functions, leading to potential execution failures.
It's crucial to design functions with the 5ms execution limit in mind, especially during high traffic periods like Black Friday.

Related Concepts

Webassembly
Javascript
Shopify Functions
Quickjs
Wasi