Skip to content

Commit 9347af3

Browse files
authored
Add js import enum benchmark (#3906)
1 parent a9f0670 commit 9347af3

File tree

7 files changed

+51
-11
lines changed

7 files changed

+51
-11
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ yarn.lock
1010
/publish.exe
1111
.vscode
1212
webdriver.json
13+
benchmarks/pkg

benchmarks/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ authors = ["The wasm-bindgen Developers"]
55
rust-version = "1.57"
66

77
[dependencies]
8-
wasm-bindgen = "0.2.43"
9-
web-sys = { version = "0.3.20", features = ['Node'] }
8+
wasm-bindgen = { path = '../' }
9+
web-sys = { path = '../crates/web-sys', features = ['Node'] }
10+
js-sys = { path = '../crates/js-sys' }
1011

1112
[lib]
1213
crate-type = ['cdylib']

benchmarks/README.md

+2-9
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,10 @@ performance suite for WebAssembly for Rust.
1111

1212
## Building and Running
1313

14-
First, copy the benchmarks to a temporary directory:
15-
16-
```
17-
$ cp ./benchmarks /some/other/directory
18-
```
19-
20-
Next, `cd` into that directory and execute:
21-
2214
```
15+
$ cd benchmarks
2316
$ cargo build --release --target wasm32-unknown-unknown
24-
$ wasm-bindgen --out-dir pkg --target web ./target/wasm32-unknown-unknown/release/wasm_bindgen_benchmark.wasm
17+
$ cargo run --package wasm-bindgen-cli -- --out-dir pkg --target web ../target/wasm32-unknown-unknown/release/wasm_bindgen_benchmark.wasm
2518
```
2619

2720
Next, use your favorite static file server to host the current directory. For

benchmarks/globals.js

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
export function jsthunk() {}
22
export function add(a, b) { return a + b; }
3+
export function use_baz(baz) {
4+
if (baz !== Baz['variant-2']) {
5+
throw new Error("Passed wrong variant");
6+
}
7+
}
38
export class Foo {
49
bar() {}
510
}
11+
12+
export const Baz = {
13+
'variant-1': 'variant-1',
14+
'variant-2': 'variant-2',
15+
'variant-3': 'variant-3',
16+
}

benchmarks/index.html

+14
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,20 @@ <h1>wasm-bindgen benchmarks</h1>
275275
<td class='bm' id='wbindgen_call_foo_bar_structural_n_times'></td>
276276
</tr>
277277

278+
<tr>
279+
<td>
280+
Call a custom JS function with an enum value parameter
281+
282+
<a class='about-open' href='#'>(?)</a>
283+
284+
<p class='about'>
285+
Benchmarks the speed of passing enum values to javascript
286+
</p>
287+
</td>
288+
289+
<td class='bm' id='wbindgen_call_use_baz_n_times'></td>
290+
</tr>
291+
278292
<tr style='display:none' class='str-benchmark'>
279293
<td>
280294
Pass <span class='str'></span> to/from wasm-bindgen

benchmarks/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import wbindgen_init, {
1616
call_first_child_final_n_times as wbindgen_call_first_child_final_n_times,
1717
call_first_child_structural_n_times as wbindgen_call_first_child_structural_n_times,
1818
call_foo_bar_final_n_times as wbindgen_call_foo_bar_final_n_times,
19+
call_use_baz_n_times as wbindgen_call_use_baz_n_times,
1920
call_foo_bar_structural_n_times as wbindgen_call_foo_bar_structural_n_times,
2021
str_roundtrip as wbindgen_str_roundtrip,
2122
} from './pkg/wasm_bindgen_benchmark.js';
@@ -80,6 +81,7 @@ function makeBenchmarks() {
8081
const foo = new globals.Foo();
8182
benchmarks.wbindgen_call_foo_bar_final_n_times = () => wbindgen_call_foo_bar_final_n_times(10000, foo);
8283
benchmarks.wbindgen_call_foo_bar_structural_n_times = () => wbindgen_call_foo_bar_structural_n_times(10000, foo);
84+
benchmarks.wbindgen_call_use_baz_n_times = () => wbindgen_call_use_baz_n_times(10000);
8385

8486

8587
const strings = {

benchmarks/src/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ extern "C" {
1111
#[wasm_bindgen(js_name = add)]
1212
fn js_add(a: i32, b: i32) -> i32;
1313

14+
#[wasm_bindgen(js_name = use_baz)]
15+
fn js_use_baz(val: Baz);
16+
1417
pub type Foo;
1518
#[wasm_bindgen(method, final, js_name = bar)]
1619
fn bar_final(this: &Foo);
@@ -23,6 +26,14 @@ extern "C" {
2326
fn doesnt_throw_catch() -> Result<(), JsValue>;
2427
}
2528

29+
#[wasm_bindgen]
30+
#[derive(Copy, Clone)]
31+
pub enum Baz {
32+
Variant1 = "variant-1",
33+
Variant2 = "variant-2",
34+
Variant3 = "variant-3",
35+
}
36+
2637
#[wasm_bindgen]
2738
pub fn call_js_thunk_n_times(n: usize) {
2839
for _ in 0..n {
@@ -81,6 +92,13 @@ pub fn call_foo_bar_structural_n_times(n: usize, js_foo: &Foo) {
8192
}
8293
}
8394

95+
#[wasm_bindgen]
96+
pub fn call_use_baz_n_times(n: usize) {
97+
for _ in 0..n {
98+
js_use_baz(Baz::Variant2);
99+
}
100+
}
101+
84102
#[wasm_bindgen]
85103
pub fn call_doesnt_throw_n_times(n: usize) {
86104
for _ in 0..n {

0 commit comments

Comments
 (0)