Git Product home page Git Product logo

wasm-vs-js-benchmark's Introduction

WebAssembly vs Javascript

Build Status Dependabot Status

A comparison between WebAssembly and Javascript made for studying fun.

What's in the box

  • WebAssembly with Rust
  • Benchmark with fancy charts
  • Web Workers to avoid a frozen main thread
  • Build and deploy with Travis CI

Rust and WebAssembly

There are two books that fully cover this section:

In this project I'm also using wasm-bingen and wasm-pack.

Project

Folder Structure

Source code is splitted into two main folders: src that contains our Rust application and src-js that contains our Javascript application.

I tried to make their folder structure as similar as possible:

src
├── libs
│   ├── mod.rs
│   ├── ...
│   └── primes.rs
└── main.rs

src-js
├── libs
│   ├── mod.js
│   ├── ...
│   └── primes.js
├── ...
├── bootstrap.js
└── main.js

*/libs/mod.* is the entry point for declaring all modules. The .rs version of this file will be compiled to .wasm.

*/libs/primes.* is a module example, written both in Rust and Javascript.

*/main.* is the entry point for the application. The .rs file is the one used by cargo run command. The .js file is the one used by webpack.

./src-js/bootstrap.js is the bootstrap file for the web application that loads the main.js file asynchronously.

Rust

The first step is to install Rust. We’ll download Rust through rustup, a command line tool for managing Rust versions and associated tools.

Runnig following command we'll install Rust and Cargo automatically. You will also need to install wasm-pack.

$ curl https://sh.rustup.rs -sSf | sh

# install `wasm-pack`
$ curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

# update dependencies
$ cargo update

Now we are able to use the following commands from our project folder.

# test .rs files
$ cargo test

# compile `src/main.rs`
$ cargo build
    Finished dev [unoptimized + debuginfo] target(s)

# or compile `src/main.rs` with optimizations
$ cargo build --release
    Finished release [optimized] target(s)

Now that we have built our code, we can run it:

$ ./target/release/wasm-vs-js-benchmark primes-get_primes 11

We can also use cargo run to compile and then run it, all in one step:

$ cargo run primes-get_primes 11

# compile and run our project with optimizations
$ cargo run --release primes-get_primes 11

Last but not least, we'll compile our project to .wasm:

$ wasm-pack build

Now we are able to use the content of ./pkg folder for our web application.

Javascript

The goal of this project is benchmarking WebAssembly and Javascript. Some task will use big computations that takes time to execute.

Web Workers makes it possible to run a script operation in a background thread separate from the main execution thread of a web application.

The web application is bundled with Webpack.

Run following command to start it:

$ npm run build:wasm
$ npm start

Project is running at http://localhost:8080/

Unit Test

You can run unit test for Rust and Javascript, simply running npm test.

I use mocha for javascript unit test, transpiling ES6 with @babel/core, @babel/preset-env and @babel/register.

Benchmark

Rust to Native Code

The execution time of .wasm binaries is just a bit slower than the execution of same native code.

For benchmarking the native code, I use Hyperfine.

These benchmarks are recorded on a MacBook Pro (15-inch, 2016) having these specs:

  • macOS Mojave
  • 2,6 GHz Intel Core i7 (4 cores)
  • 16 GB 2133 MHz LPDDR3
$ cargo build --release

$ hyperfine --warmup 3 --export-markdown BENCHMARK.md \
    './target/release/wasm-vs-js-benchmark primes-get_primes 100000' \
    './target/release/wasm-vs-js-benchmark matrix-multiply 500 500'
Command Mean [s] Min…Max [s]
./target/release/wasm-vs-js-benchmark primes-get_primes 100000 1.211 ± 0.018 1.196…1.255
./target/release/wasm-vs-js-benchmark matrix-multiply 500 500 0.435 ± 0.016 0.417…0.469

WebAssembly vs Javascript

https://marcomontalbano.github.io/wasm-vs-js-benchmark

Useful Links

wasm-vs-js-benchmark's People

Contributors

dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar marcomontalbano avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

wasm-vs-js-benchmark's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.