From 90f1866ddd6a340acc786c7da3c37d9f98fbd58d Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Wed, 22 May 2019 09:39:08 -0700 Subject: [PATCH 1/2] Add note to the guide about `serde-wasm-bindgen` --- .../reference/arbitrary-data-with-serde.md | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/guide/src/reference/arbitrary-data-with-serde.md b/guide/src/reference/arbitrary-data-with-serde.md index 070a61e4..03161f30 100644 --- a/guide/src/reference/arbitrary-data-with-serde.md +++ b/guide/src/reference/arbitrary-data-with-serde.md @@ -1,8 +1,8 @@ # Serializing and Deserializing Arbitrary Data Into and From `JsValue` with Serde It's possible to pass arbirtrary data from Rust to JavaScript by serializing it -with [Serde](https://github.com/serde-rs/serde). `wasm-bindgen` includes the -`JsValue` type, which streamlines serializing and deserializing. +to JSON with [Serde](https://github.com/serde-rs/serde). `wasm-bindgen` includes +the `JsValue` type, which streamlines serializing and deserializing. ## Enable the `"serde-serialize"` Feature @@ -105,3 +105,23 @@ example.field2.push([5,6]); // Send the example object back to wasm. receive_example_from_js(example); ``` + +## An Alternative Approach: `serde-wasm-bindgen` + +[The `serde-wasm-bindgen` +crate](https://github.com/cloudflare/serde-wasm-bindgen) serializes and +deserializes Rust structures directly to `JsValue`s, without going through +temporary JSON stringification. This approach has both advantages and +disadvantages. + +The primary advantage is smaller code size: going through JSON entrenches code +to stringify and parse floating point numbers, which is not a small amount of +code. + +There are two primary disadvantages. The first is that it is not always +compatible with the default JSON-based serialization. The second is that it +performs more calls back and forth between JS and Wasm, which has not been fully +optimized in all engines, meaning it can sometimes be a speed +regression. However, in other cases, it is a speed up over the JSON-based +stringification, so — as always — make sure to profile your own use +cases as necessary. From 2a896dbeb3e163b1c015849a4b3615a0657abb54 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Thu, 23 May 2019 14:14:21 -0700 Subject: [PATCH 2/2] Also mention that serde-wasm-bindgen supports more types than JSON does --- guide/src/reference/arbitrary-data-with-serde.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/guide/src/reference/arbitrary-data-with-serde.md b/guide/src/reference/arbitrary-data-with-serde.md index 03161f30..9d1109f5 100644 --- a/guide/src/reference/arbitrary-data-with-serde.md +++ b/guide/src/reference/arbitrary-data-with-serde.md @@ -116,7 +116,8 @@ disadvantages. The primary advantage is smaller code size: going through JSON entrenches code to stringify and parse floating point numbers, which is not a small amount of -code. +code. It also supports more types than JSON does, such as `Map`, `Set`, and +array buffers. There are two primary disadvantages. The first is that it is not always compatible with the default JSON-based serialization. The second is that it