As the popularity of JavaScript has improved over the years, desktop applications have increasingly started moving away from thick-clients using languages such as .NET and Java to thin-clients like those built out with JavaScript. Frameworks such as OpenFin and Electron have exploded in growth in recent years because of this trend. However, browser-based JavaScript applications do not have the ability to call native code so browser-based JavaScript applications have been relegated, for the most part, to the presentation layer while more heavy weight processing has been moved to the server side. While this works well for a lot of the use cases as desktop and laptop computers continue to get upgraded with more cores, a tremendous amount of processing capability is being left unused.

In this blog post, I will explain how using technology like Rust, WebAssembly, JavaScript (via the Svelte framework), and Solace PubSub+ enables you to reclaim some of that processing power while you continue to use your modern HTML5 applications through a very basic implementation of an options pricer in the browser.

Whew … those were a lot of buzzwords. Let’s start by breaking each of them down.

Rust

Rust is an exciting new programming language from the fine folks over at the Mozilla foundation. It’s a low-level language that offers extreme performance with no run-time since it targets the LLVM. This makes it ideal for embedded/low powered devices. Other notable features are the lack of a garbage collector, strong compile time checks (such as the borrow checker), support for functional programming constructs (such as closures), and object oriented constructs (via traits). If you’d like to learn more, I highly recommend the Rust Book.

WebAssembly

WebAssembly is a binary instruction format that is designed as a compilation target for programming languages allowing them to be deployed onto the web. It also allows for inter-op with JavaScript through a wasm.js module that you can load in your client-side applications. This allows you to take advantage of native code in your web applications.

Rust also includes first-class support for compiling down to WebAssembly. With its strong memory safety paradigms, it is an ideal language for this purpose.

Svelte

Svelte is a no-nonsense JavaScript framework. It primarily differs from the more popular frameworks such as Angular, Vue, and ReactJS due to the fact that it compiles down to pure JavaScript without the need for a heavy framework library to be loaded, making it blazingly fast and the resultant apps extremely small.

Solace PubSub+

Solace PubSub+ is an advanced event broker that has many advanced features such as the ability to form an event-mesh, native multi-protocol support, and it’s FREE! Solace PubSub+ will form a core part of the architecture facilitating communication between WebAssembly, JavaScript, and a Rust process.

An Example Using Options and Options Pricing

An option is a derivative financial instrument that gives the buyer the right to purchase/sell an asset at a specific price. It also allows for the seller of the options contract to generate income on assets that they have in their portfolio by taking a view on whether they see an upswing or downswing in the price of an asset.

Let’s take an example. Say you think the price of AAPL is going to go above $125 from its current price of $100 after they announce the next iPhone. In such a situation, you can buy a CALL option for a strike price at $125 that expires at the end of September. What this means is that if by the end of September, the price of the AAPL stock reaches $150, having the option contract gives you the right to buy AAPL stock at $125 and you can instantly pocket the profit of $25 by selling AAPL back at $150 (minus how much it costs to purchase the option contract of course). However, if the price never reaches $125 by the end of September, your investment in the option becomes worthless and you lost your money. Likewise, a PUT option gives you the ability to sell an asset at a price in case you believe the asset will go down by a certain amount.

Pricing an option is not a straightforward task as there are multiple factors associated with the price known as ‘The Greeks’. While the math behind pricing an option goes beyond the scope of this blog post, I recently stumbled upon a blog post by Ronnie Chowdhury entitled Probably the fastest Black Scholes Pricer in the world. I recommend reading through the post but the most interesting technical bit of information for me was a topic called SIMD that allows the CPU to perform multiple operations with one instruction set.

With SIMD, instead we could instead act on multiple numbers together.

From the above we are iterating over each value and adding them one by one. But you would hope the compiler is clever enough to see that actually there’s an instruction _mm_add_ps() that could do 8 additions in 1 instruction. That’s 8x faster.”

I was able to make some small changes to Ronnie’s code which you can find here to allow it to be compiled to WebAssembly.

Bringing it all together

So, we have a highly performant options pricer implemented in Rust. I also created a Svelte WebApp that compiles the Rust code to WebAssembly and creates a binding to call this code from JavaScript. I also implemented a very bare-bones Rust Options Market Data Simulator that publishes option prices to Solace PubSub+ which you can find here. The Svelte app will consume these feeds from a Solace PubSub+ Event Broker and make a call to WebAssembly to price the option feed (note: the simulator spits out random data so the option prices generated by the pricer will not be realistic).

Here is an image of the application in action:

To run this yourself, follow the instructions in this repo.

Conclusion

While WebAssembly interop with JavaScript is arguably in its infancy and will see tighter integration and improved performance over time, hopefully the information and example in this post opens up your mind to the possibilities of using more of your desktop/laptop cores that are sitting idle on your computer for your modern web applications.

Thomas Kunnumpurath

Thomas Kunnumpurath is the Vice President of Systems Engineering for Americas at Solace where he leads a field team across the Americas to solution the Solace PubSub+ Platform across a wide variety of industry verticals such as Finance, Retail, IoT and Manufacturing.

Prior to joining Solace, Thomas spent over a decade of his career leading engineering teams responsible for building out large scale globally distributed systems for real time trading systems and credit card systems at various banks.

Thomas enjoys coding, blogging about tech, speaking at conferences and being invited to talk on PodCasts. You can follow him at Twitter with the handle @TKTheTechie, GitHub @TKTheTechie and his blog on TKTheTechie.io

Join Our Developer Community

Join the Solace Developer Community to discuss and share PubSub+ API hints, new features, useful integrations, demos, and sample code!

JOIN THE DISCUSSION