Skip to content

Commit e37eb63

Browse files
authored
Browser support + marginally smaller modules (#1)
* Shave ~20 bytes from math.js file This is just a small micro-optimization using converge and inline.always to remove a few bytes off of the final .wasm size. Sadly, we can't use "shrinkLevel": 3 since that gives imprecise results. * Fix the message for the 10^-5 test This confused me for a couple minutes when testing out "shrinkLevel": 3. * Use `fetch` and `WebAssembly.instantiateStreaming`` to support browsers Browsers don't have Node.js's Buffer, nor do they have any reasonable way to decode base64. Luckily, `WebAssembly.instantiateStreaming` allows using the results of `fetch`, which in turn can accept base64 data URIs.
1 parent 0e54be8 commit e37eb63

File tree

5 files changed

+5
-8
lines changed

5 files changed

+5
-8
lines changed

asconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"debug": false,
77
"noExportMemory": true,
88
"optimize": true,
9+
"converge": true,
910
"noAssert": true
1011
}
1112
}

assembly/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export function f64_pow(x: f64, y: f64): f64 {
2-
return Math.pow(x, y);
2+
return inline.always(Math.pow(x, y));
33
}

index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
import { MathWasmBase64 } from "./build/math.js";
22

3-
const exports = (await WebAssembly.instantiate(Buffer.from(MathWasmBase64, "base64"))).instance.exports;
4-
5-
export function f64_pow(value, exponent) {
6-
return exports.f64_pow(value, exponent);
7-
}
3+
export const {f64_pow} = (await WebAssembly.instantiateStreaming(fetch(MathWasmBase64))).instance.exports;

scripts/postbuild.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import { readFileSync, writeFileSync } from "fs";
22

33
const base64String = readFileSync("build/math.wasm", "base64");
44

5-
writeFileSync("build/math.js", `export const MathWasmBase64 = "${base64String}";`);
5+
writeFileSync("build/math.js", `export const MathWasmBase64 = "data:application/wasm;base64,${base64String}";`);

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import { f64_pow } from "../index.js";
22
import assert from "assert";
33

44
assert(f64_pow(2, 2) === 4, "2^2 == 4");
5-
assert(f64_pow(10, -5) === 0.00001, "2^2 == 4");
5+
assert(f64_pow(10, -5) === 0.00001, "10^-5 == 0.00001");

0 commit comments

Comments
 (0)