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

Commit

Permalink
Merge pull request #68 from libp2p/feat/fast-copy
Browse files Browse the repository at this point in the history
feat: faster copy in wasm
  • Loading branch information
Stebalien committed Jan 9, 2020
2 parents 79f4bb3 + 9a9b037 commit 709d2b3
Showing 1 changed file with 5 additions and 5 deletions.
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

0 comments on commit 709d2b3

Please sign in to comment.