Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

feat: faster copy in wasm #68

Merged
merged 1 commit into from
Jan 9, 2020
Merged
Changes from all commits
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
10 changes: 5 additions & 5 deletions conn_browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ func (c *Conn) Write(b []byte) (n int, err error) {
return 0, err
}
uint8Array := js.Global().Get("Uint8Array").New(len(b))
for i, bi := range b {
uint8Array.SetIndex(i, bi)
if js.CopyBytesToJS(uint8Array, b) != len(b) {
panic("expected to copy all bytes")
}
c.Call("send", uint8Array.Get("buffer"))
return len(b), nil
Expand Down Expand Up @@ -262,10 +262,10 @@ func (c *Conn) waitForOpen() error {
// arrayBufferToBytes converts a JavaScript ArrayBuffer to a slice of bytes.
func arrayBufferToBytes(buffer js.Value) []byte {
view := js.Global().Get("Uint8Array").New(buffer)
dataLen := view.Get("length").Int()
dataLen := view.Length()
data := make([]byte, dataLen)
for i := 0; i < dataLen; i++ {
data[i] = byte(view.Index(i).Int())
if js.CopyBytesToGo(data, view) != dataLen {
panic("expected to copy all bytes")
}
return data
}
Expand Down