Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Working With Uint8Array

Working With Uint8Array

In this lesson, you read and write single bytes in a Uint8Array, then take part of it with and without a copy. In the last lesson, a TextEncoder encoded Zoë as four bytes: 90, 111, and then 195 and 171 for the ë. The helper now holds them in a Uint8Array, and two of its habits would surprise the team lead’s code.

The team lead’s Python keeps those bytes in a bytearray, so it can change them. It reads one byte by index, 195, the first of the ë’s two bytes. Then, to watch one byte change, it writes 122, a lowercase z, over the first byte. Real lowercasing happens on the string, before encoding:

Language: Python
data = bytearray([90, 111, 195, 171])

print(data[2])
data[0] = 122
print(list(data))

This prints 195 and [122, 111, 195, 171]. In JavaScript, the four bytes go into a Uint8Array with new and an array literal. Indexing works as on an array: square brackets, counting from zero, as in Python. Unlike Python, though, a negative index doesn’t count from the end. Writing works the same way: assign to an index, and the byte changes in place:

Language: JavaScript
const data = new Uint8Array([90, 111, 195, 171]);

console.log(data[2]);
data[0] = 122;
console.log(data);

The log shows 195, then the lowercase z’s 122 at the front, and the (4) says the length is still four:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In this lesson, you’ll read and write single bytes in a Uint8Array, then take part of it with and without a copy. In the last lesson, a TextEncoder encoded Zoë as four bytes, the last two for the ë.

00:16 The helper now holds them in a Uint8Array, and two of its habits would surprise the team lead’s code. The team lead’s Python keeps those bytes in a bytearray, so it can change them.

00:29 It reads one byte by index: 195, the first of the ë’s two bytes. Then, to watch one byte change, it writes 122, a lowercase z, over the first byte. Real lowercasing happens on the string, before encoding.

00:48 In JavaScript, a Uint8Array gets the same four values from new and an array literal. Indexing works as on an array: square brackets, counting from zero, as in Python.

01:02 Unlike Python, though, a negative index doesn’t count from the end. So the port’s first log reads 195 again.

01:11 Writing works the same way: assign to an index, and the byte changes in place. The log shows 122 at the front, the lowercase z, and the length in parentheses is still four.

01:25 A byte holds a whole number from 0 to 255. Say a value computed elsewhere comes out as 256, and the Python version writes it into the last byte. Python stops it: the bytearray raises a ValueError, because a byte must be in range.

01:44 The JavaScript port writes 256 into the same index, then logs that byte. Does JavaScript raise an error like Python, store 256 as a plain array would, or log something else?

01:58 Your turn: pause the video and predict.

02:06 No error, and no 256: it logs zero.

02:13 Why zero? A Uint8Array element is eight bits, and JavaScript converts a number that doesn’t fit. Past 255, the count wraps around to zero, like a car’s odometer.

02:27 JavaScript keeps the remainder after dividing by 256, what Python writes as percent 256. So 256 becomes zero, and 300 becomes 44, because 300 minus 256 is 44.

02:43 Python checks the range and raises; JavaScript keeps the remainder and says nothing.

02:50 When numbers can pass 255, such as a sum or a count, a JavaScript developer keeps them in a plain array until each one is checked. The plain array stores each number as given: this one logs 256.

03:05 Write only checked values, 0 to 255, into a Uint8Array: unlike a bytearray, it won’t refuse the rest.

03:16 Back to Zoë’s four bytes: sometimes you need part of them, such as the first two. In Python, slicing a bytearray makes a copy, and JavaScript’s .slice() does the same, with the positions it takes on strings.

03:30 The copy gets two new boxes of its own.

03:34 The new method, .subarray(), takes the same positions but makes no copy. It’s a view, a window onto the original’s own bytes. Python’s memoryview looks at a bytearray without copying it, so its slice is a view too.

03:50 Watch out: unlike a Python slice, a subarray isn’t a copy, so whatever you write through it ends up in the original. Unlike a subarray, a memoryview still refuses 256.

04:05 Now write 122 through the view, at the view’s own index zero, then log the original and the copy, one per line. The original shows the lowercase z at the front, because the view and the original share those bytes.

04:20 The copy still starts with 90, because it has bytes of its own. So use .subarray() to work on part of the bytes in place, and .slice() for a copy of your own.

04:33 So a Uint8Array indexes like a bytearray, but keeps the remainder of a whole number that doesn’t fit, where Python raises. .slice() copies bytes, and .subarray() shares them.

04:46 Typed arrays go further, with other number sizes and shared buffers, but bytes are all a branch name needs.

04:54 Next up is the exercise Fit a Branch Name Under the Host’s Byte Limit. There, you’ll cut a branch name to the longest prefix of whole code points that fits the host’s byte budget.

Become a Member to join the conversation.