Skip to content

Instantly share code, notes, and snippets.

@petamoriken
Created August 7, 2021 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petamoriken/cd8810084553912ab1af137af181ca47 to your computer and use it in GitHub Desktop.
Save petamoriken/cd8810084553912ab1af137af181ca47 to your computer and use it in GitHub Desktop.
// TypedArray constructor
// Array
const uint8_1 = new Uint8Array([1, 2, 3]);
console.log(uint8_1);
// => Uint8Array(3) [1, 2, 3]
// ArrayLike
const uint8_2 = new Uint8Array({ 0: 1, 1: 2, 2: 3, length: 3 });
console.log(uint8_2);
// => Uint8Array(3) [1, 2, 3]
// Iterable
const uint8_3 = new Uint8Array([1, 2, 3].values());
console.log(uint8_3);
// => Uint8Array(3) [1, 2, 3]
// TypedArray.from
// Array
const uint8_4 = Uint8Array.from([1, 2, 3]);
console.log(uint8_4);
// => Uint8Array(3) [1, 2, 3]
// ArrayLike
const uint8_5 = Uint8Array.from({ 0: 1, 1: 2, 2: 3, length: 3 });
console.log(uint8_5);
// => Uint8Array(3) [1, 2, 3]
// Iterable
const uint8_6 = Uint8Array.from([1, 2, 3].values());
console.log(uint8_6);
// => Uint8Array(3) [1, 2, 3]
// TypedArray#set
// Array
const uint8_7 = new Uint8Array(3);
uint8_7.set([1, 2, 3]);
console.log(uint8_7);
// => Uint8Array(3) [1, 2, 3]
// ArrayLike
const uint8_8 = new Uint8Array(3);
uint8_8.set({ 0: 1, 1: 2, 2: 3, length: 3 });
console.log(uint8_8);
// => Uint8Array(3) [1, 2, 3]
// Iterable
const uint8_9 = new Uint8Array(3);
uint8_9.set([1, 2, 3].values());
console.log(uint8_9);
// => Uint8Array(3) [0, 0, 0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment