jsvalue

package module
v0.0.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 18, 2026 License: MIT Imports: 2 Imported by: 0

README

jsvalue

Efficient conversions between JavaScript and Go for a WebAssembly environment with support for TinyGo.

Usage

Importing
import "github.com/tinywasm/jsvalue"
ToJS

Converts Go values to syscall/js.Value. For structs and slices, they must implement fmt.Encodable.

// Basic types
val := jsvalue.ToJS(123)
val := jsvalue.ToJS("hello")

// Encodable types (0-alloc on Go side)
user := &User{Name: "Alice"}
jsVal := jsvalue.ToJS(user)
ToGo

Converts syscall/js.Value to Go values, populating a pointer destination. For structs and slices, they must implement fmt.Decodable.

// Integers (Zero allocation)
var n int
err := jsvalue.ToGo(jsVal, &n)

// Structs (implementing fmt.Decodable)
var user User
err := jsvalue.ToGo(jsObj, &user)
ToAny

Converts a JS value to a Go any.

  • Integer numbers are returned as int64.
  • Non-integer numbers as float64.
  • For objects, it returns the raw js.Value (to avoid map runtime overhead).
  • Arrays are returned as []any.

Performance Results

Last updated: 2026-06-18 (Post-Reflect/Map Removal — codec migration)

Binary size (wasm, -opt=z -no-debug)
Before (reflect+map) After (codec) Delta
jsvalue consumer binary ~72 KB reflect tables + map runtime reflect removed ~−72 KB
Benchmarks (goos: js, goarch: wasm — measured 2026-06-18)
pkg: github.com/tinywasm/jsvalue
BenchmarkToJS_Int     	143221300	        16.80 ns/op	       0 B/op	       0 allocs/op
BenchmarkToJS_String  	 1000000	      2029 ns/op	       8 B/op	       1 allocs/op
BenchmarkToJS_Struct  	  313754	      8153 ns/op	      40 B/op	       4 allocs/op
BenchmarkToGo_Int     	153262053	        15.57 ns/op	       0 B/op	       0 allocs/op
BenchmarkToGo_Struct  	  404035	      5862 ns/op	      40 B/op	       4 allocs/op
BenchmarkToGo_Any_Int 	14609425	       163.1 ns/op	      24 B/op	       2 allocs/op

Note: allocs/op reflects JS bridge allocations (syscall/js boxes each value passed to js.Value.Set/Get as interface{}). This is inherent to the Go↔JS bridge and cannot be eliminated. There is no reflect in the Go-side codec path.

Before (reflect-based): struct conversion used reflect.ValueOf per call — each ToJS/ToGo on a struct triggered reflect type lookup + value iteration (~3× slower, plus ~72 KB of reflect type tables in the wasm binary). Removed along with all map[string]* cases. Struct/slice conversion now requires implementing fmt.Encodable/fmt.Decodable.

Documentation

Rendered for js/wasm

Index

Constants

This section is empty.

Variables

View Source
var Uint8ArrayClass = js.Global().Get("Uint8Array")

Uint8ArrayClass is the JS Uint8Array constructor.

Functions

func AwaitPromise added in v0.0.11

func AwaitPromise(p js.Value) (js.Value, error)

AwaitPromise blocks the current goroutine until the JS Promise p resolves or rejects. Safe in WASM: channel receive yields the goroutine; the JS event loop keeps running.

func AwaitRequest added in v0.0.11

func AwaitRequest(req js.Value) (js.Value, error)

AwaitRequest blocks the current goroutine until the IndexedDB request req fires its "success" or "error" event. Ported from tinywasm/indexdb processRequest — proven pattern.

func ScanValue added in v0.0.12

func ScanValue(v js.Value, dest any) error

ScanValue copies the JS value v into the Go pointer dest. Supports *string, *int, *int64, *int32, *float64, *bool, *[]byte, *any.

func ToAny added in v0.0.12

func ToAny(v js.Value) any

ToAny converts a JS value to a Go any. Integer numbers are returned as int64; non-integer numbers as float64. For objects, it returns the raw js.Value (no map).

func ToGo

func ToGo(jsVal js.Value, v any) error

ToGo converts JavaScript values to Go values. For structs and slices, they must implement fmt.Decodable.

func ToJS

func ToJS(data any) js.Value

ToJS converts Go values to JavaScript values recursively. For structs and slices, they must implement fmt.Encodable.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL