Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adding comments and photo to mini-benchmark readme
  • Loading branch information
mini-eggs committed Jun 6, 2017
commit bceaf5727c1ca49bc0069d9b2540ea7ce47c4326
3 changes: 3 additions & 0 deletions examples/mini-benchmark/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Mini Benchmark
Check how long making a simple addition one million times takes in JavaScript vs Webassembly.

![](https://i.imgur.com/35yItYN.png)

#### Setup
$ npm install

#### Development build
$ npm start

Open http://localhost:5000/

#### Production build
Expand Down
12 changes: 12 additions & 0 deletions examples/mini-benchmark/src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,34 @@ const bench = 1000000;

(async function() {
try {
/**
* Destructure add func from WASM file.
*/
const { exports: { add } } = await wasm.load("program.wasm");

/**
* JavaScript benchmark.
*/
let start = new Date().getTime();
for (let e = 0; e < bench; e++) {
const result = numOne + numTwo;
}
let end = new Date().getTime();
const timeOne = end - start;

/**
* WebAssembly benchmark.
*/
start = new Date().getTime();
for (e = 0; e < bench; e++) {
const result = add(numOne, numTwo);
}
end = new Date().getTime();
const timeTwo = end - start;

/**
* Display results.
*/
if (timeOne > timeTwo) {
alert(`WASM was faster by ${Math.round(timeOne / timeTwo * 100)}%`);
} else {
Expand Down