GUIDES Optimizing Embedded JavaScript Avoid Copying Buffers
About
Binary Data
DNS
DNS Service Discovery
EventSource
Files
HTTP
Implementing ECMA-419 Modules
Key-Value Storage
Logging
MQTT
Optimizing Embedded JavaScript
Streams
Time
Time Callbacks
Transport Layer Security (TLS)
WebSocket
Wi-Fi
Overview
When to Optimize
Know Where to Optimize
Loop through an Array
Iterate Over a String
Build a String
Avoid Copying Buffers
Accessing Properties
Map versus Object
Append to an Array
Operate on Bits
Define Class Methods
About
Binary Data
Overview
Convert String to ArrayBuffer
Convert ArrayBuffer to String
Convert ArrayBuffers to String
Handle Errors Converting ArrayBuffer to String
Immutable ArrayBuffers
Resize an ArrayBuffer
Combine ArrayBuffers
Convert Base64 to Binary Data
Convert Binary Data to Base64
Convert Binary Data to Hex
Convert Hex to Binary Data
Calculate CRC for Binary Data
Compress Binary Data – One Buffer
Compress Binary Data – Streaming
Decompress Binary Data – One Buffer
Decompress Binary Data – Streaming
DNS
Overview
Resolve Name
Resolve Multiple Names
DNS Service Discovery
Overview
Claim Local Name
Advertise Services
Discover Services
EventSource
Overview
Connect
Connect Securely
Close
Receive
Connection Information
Files
Overview
Create, Open, and Close File
Read File
Write File
Delete File
Get File Information
Create Directory
Enumerate Directory
Delete Directory
Open Directory
HTTP
Overview
Make Request using fetch()
Make Secure Request using fetch()
Send Request Headers using fetch()
Receive Response Headers using fetch()
Send Request Body using fetch()
Make Request using HTTP Client
Make Secure Request using HTTP Client
Send Request Headers using HTTP Client
Receive Response Headers using HTTP Client
Send Request Body using HTTP Client
Implementing ECMA-419 Modules
Overview
Constructor Sets `target`
Constructor IO
Constructor Clean-up on Failure
`close()` and `[Symbol.dispose]`
Calling Callbacks
Setting Options with `configure()`
Keep Instance Surface Clean
Key-Value Storage
Overview
Read and Write Values using Web Storage
Delete Keys using Web Storage
Enumerate Keys using Web Storage
Read and Write Values using Key-Value Pair
Change Data Formats using Key-Value Pair
Delete Keys using Key-Value Pair
Enumerate Keys using Key-Value Pair
Logging
Overview
Logging with Console
Logging with trace()
MQTT
Overview
Connect to MQTT Server using MQTT()
Connect Securely to MQTT Server using MQTT()
Close Connection using MQTT()
Publish Message using MQTT()
Subscribe to Topic using MQTT()
Receive Messages using MQTT()
Get Connection Information using MQTT()
Connect to MQTT Server using MQTT Client
Connect Securely to MQTT Server using MQTT Client
Close Connection using MQTT Client
Publish Message using MQTT Client
Subscribe to Topic using MQTT Client
Receive Messages using MQTT Client
Optimizing Embedded JavaScript
Overview
When to Optimize
Know Where to Optimize
Loop through an Array
Iterate Over a String
Build a String
Avoid Copying Buffers
Accessing Properties
Map versus Object
Append to an Array
Operate on Bits
Define Class Methods
Streams
Overview
Time
Overview
Get Unix Time
Get Time of Day
Get Date
Get Time Since System Start
Get Microseconds
Set System Date and Time
Get Time and Date from Real-Time Clock
Set Real-Time Clock Time
Get Time and Date from Network
Sleep
Time Callbacks
Overview
One-Time Callback
Repeating Callback
Repeating Callback with Initial Delay
Immediate Callback
Reschedule Callback
Cancel Callback
Suspend Callback
Transport Layer Security (TLS)
Overview
Include Public Certificates
Include Private Certificates
Diagnostics
DER and PEM Certificates
WebSocket
Overview
Connect to Server using WebSocket()
Connect Securely to Server using WebSocket()
Close Connection using WebSocket()
Send Message using WebSocket()
Receive Message using WebSocket()
Get Connection Information using WebSocket()
Connect to Server using WebSocket Client
Connect Securely to Server using WebSocket Client
Close Connection using WebSocket Client
Send Message using WebSocket Client
Receive Message using WebSocket Client
Control Messages using WebSocket Client
Connect to Server using WebSocketStream
Connect Securely to Server using WebSocketStream
Close Connection using WebSocketStream
Send Message using WebSocketStream
Receive Message using WebSocketStream
Wi-Fi
Overview
Scan for Access Points
Scan Continuously for Access Points
Connect
Reconnect Automatically
Disconnect
Get Connection Information
Use Static IP Address

Avoid Copying Buffers

Embedded JavaScript developers often work with binary data buffers. Making copies of data in buffers can be expensive, especially for large buffers. It requires additional memory and the copy operation takes time (and the RAM access speed on embedded devices is often not that fast). Fortunately, there are ways to avoid copying buffers.

The subarray() method of TypedArray creates a new TypedArray instance that references a part of the original TypedArray. Compare this to slice() which makes a copy of the buffer.

Because subarray() does not make a copy of the buffer, changes to the original buffer will be reflected in the subarray and changes to the subarray will be reflected on the original buffer.

/* BEFORE */
let bytes = Uint8Array.of(0, 1, 2, 3, 4, 5);
let part = bytes.slice(1, 3);
// => [1, 2]
bytes[1] = 100;
// => part[0] = 1

/* AFTER */
let bytes = Uint8Array.of(0, 1, 2, 3, 4, 5);
let part = bytes.subarray(1, 3);
// => [1, 2]
bytes[1] = 100;
// => part[0] = 100

A DataView is commonly used to work with binary data that contains several different types of data. It can be convenient to simultaneously access the underlying binary data as both a DataView and a TypedArray, for example when a Uint8Array is embedded in the binary data.

let view = getDataView();
let bytes = new Uint8Array(
	view.buffer,
	view.byteOffset + 10,
	20);
// => bytes references data from
//    offset 10 to 30 of view

You can also go the other way: creating a DataView that references data within a TypedArray:

let bytes = getUint8Array();
let view = new DataView(
	bytes.buffer,
	bytes.byteOffset + 10,
	20);
// => view references data from
//    offset 10 to 30 of bytes

You can pass buffers created using these "no copy" techniques to many standard APIs. For example, you can call the TextDecoder on bytes embedded within a DataView to extract a string. This example reads the length of the string in bytes from offset 50 in the DataView and creates a string from that number of bytes that follow.

let view = getDataView();
let stringBytes = new Uint8Array(
	view.buffer,
	view.byteOffset + 51,
	view.getUint8(50));
let decoder = new TextDecoder();
let string = decoder.decode(stringBytes);