new ArrayBuffer【コンストラクタ】
isView【ビュー判定】
slice【部分コピー】
resize【サイズ変更】
transfer【データ転送 (サイズ変更可)】
transferToFixedLength【データ転送 (サイズ変更不可)】
new ArrayBuffer【コンストラクタ】
メモ
概要
- バイナリデータの配列を生成
- 配列は 0 クリア
- オプションの最大サイズ指定:resize【サイズ変更】 でサイズ変更可
関連
外部リンク
- ECMA-262 (英語)
ArrayBuffer ( length [ , options ] ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
new ArrayBuffer( length [, options ] )
ArrayBuffer【バイナリデータ配列】
lengthサイズ (バイト単位)
optionsオプション
maxByteLength キー変更可能な最大サイズ (バイト単位)
TypeError new なしで呼び出し
RangeError
length が 0 未満
確保不可
例
const buff = new ArrayBuffer(8);
const typedArray = new Int32Array(buff)
console.log(typedArray);
// 出力:Int32Array(2) [0, 0]
console.log(typedArray.byteLength);
// 出力:8
console.log(typedArray.length);
// 出力:2
ArrayBuffer.isView【ビュー判定】
メモ
概要
- ビューか否かを判定
外部リンク
- ECMA-262 (英語)
ArrayBuffer.isView ( arg ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
ArrayBuffer.isView( arg )
判定結果
true:ビュー
false:その他
arg判定するビュー
例
const buff = new ArrayBuffer(8);
console.log(ArrayBuffer.isView(buff));
// 出力:false
const typedArray = new Int32Array(buff)
console.log(ArrayBuffer.isView(typedArray));
// 出力:true
ArrayBuffer.prototype.slice【部分コピー】
メモ
概要
- 部分コピーして新規に ArrayBuffer を作成
- 開始要素位置を0、終了要素位置を省略すれば全体コピー
関連
外部リンク
- ECMA-262 (英語)
ArrayBuffer.prototype.slice ( start, end ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
arrayBuffer.slice( start[, end] )
コピーされた新規の ArrayBuffer (end<start の場合、サイズは 0)
start開始要素位置 (詳細は下記参照)
end終了要素位置 (この位置の要素は含まない / 詳細は下記参照)
start | 説明 |
---|---|
0 ≦ | 先頭からの要素位置 (要素数 <:要素数) |
< 0 | 末尾からの要素位置 (-1:末尾) |
end | 説明 |
---|---|
省略 | 末尾まで (要素数:末尾+1) |
0 ≦ | 先頭からの要素位置 (要素数 <:要素数) |
< 0 | 末尾からの要素位置 (-1:末尾) |
例
const buff1 = new ArrayBuffer(20);
const typedArray1 = new Int32Array(buff1)
for (let i = 0; i < typedArray1.length; i++) {
typedArray1[i] = i;
}
console.log(typedArray1);
// 出力:Int32Array(5) [0, 1, 2, 3, 4]
console.log(typedArray1.length);
// 出力:5
console.log(buff1.byteLength);
// 出力:20
const buff2 = buff1.slice(4, 16);
const typedArray2 = new Int32Array(buff2)
console.log(typedArray2);
// 出力:Int32Array(3) [1, 2, 3]
console.log(typedArray2.length);
// 出力:3
console.log(buff2.byteLength);
// 出力:12
for (let i = 0; i < typedArray2.length; i++) {
typedArray2[i] += 10;
}
console.log(typedArray1);
// 出力:Int32Array(5) [0, 1, 2, 3, 4]
console.log(typedArray2);
// 出力:Int32Array(3) [11, 12, 13]
ArrayBuffer.prototype.resize【サイズ変更】
メモ
概要
- バッファのサイズ変更 (バイト単位)
- サイズ変更可で生成されたバッファが対象
- 縮小可
- 拡大で元データを超えた領域は 0 クリア
- データ転送元のバッファはサイズ変更不可
- 関連プロパティ
関連
外部リンク
- ECMA-262 (英語)
ArrayBuffer.prototype.resize ( newLength ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
arrayBuffer.resize( newLength )
なし
newLength変更サイズ (バイト単位)
TypeError
切り離し済
サイズ変更不可
RangeError 変更可能な最大サイズより大きい (バイト単位)
例
// サイズ変更可
const buff = new ArrayBuffer(8, { maxByteLength: 64 });
console.log(buff.resizable);
// 出力:true
console.log(buff.byteLength);
// 出力:8
console.log(buff.maxByteLength);
// 出力:64
const typedArray = new Int8Array(buff);
const size = buff.byteLength;
for (let i = 0; i < size; i++) {
typedArray[i] = i + 1;
}
console.log(typedArray);
// 出力:Int8Array(8) [1, 2, 3, 4, 5, 6, 7, 8, buffer: ArrayBuffer(8), (省略)]
buff.resize(64); // サイズ拡大
console.log(typedArray);
// 出力:Int8Array(64) [1, 2, 3, 4, 5, 6, 7, 8, 0, 0, (省略), buffer: ArrayBuffer(64), (省略)]
console.log(buff.byteLength);
// 出力:64
buff.resize(32); // サイズ縮小
console.log(buff.byteLength);
// 出力:32
// buff.resize(256); // サイズ拡大 (最大サイズオーバー)
// 例外:RangeError
// データ転送 (サイズ変更可)
buff2 = buff.transfer(4);
console.log(buff2.resizable);
// 出力:true
console.log(buff2.byteLength);
// 出力:4
console.log(buff2.maxByteLength);
// 出力:64
buff2.resize(64); // サイズ変更
console.log(buff2.byteLength);
// 出力:64
// buff.resize(64); // 切り離し済バッファサイズ変更
// 例外:TypeError
// サイズ変更不可
const buffFixed = new ArrayBuffer(8);
console.log(buffFixed.resizable);
// 出力:false
console.log(buffFixed.byteLength);
// 出力:8
console.log(buffFixed.maxByteLength);
// 出力:8
// buffFixed.resize(32); // サイズ変更
// 例外:TypeError
// データ転送 (サイズ変更不可)
buffFixed2 = buffFixed.transferToFixedLength(256);
console.log(buffFixed2.resizable);
// 出力:false
console.log(buffFixed2.byteLength);
// 出力:256
console.log(buffFixed2.maxByteLength);
// 出力:256
// buffFixed2.resize(192); // サイズ変更
// 例外:TypeError
ArrayBuffer.prototype.transfer【データ転送 (サイズ変更可)】
ArrayBuffer.prototype.transferToFixedLength【データ転送 (サイズ変更不可)】
メモ
概要
- データ転送
- データ所有権譲渡の簡素化
- サイズ変更不可のバッファを変更可に変更することは不可
- サイズ変更可:transfer【データ転送 (サイズ変更可)】
- サイズ変更不可:transferToFixedLength【データ転送 (サイズ変更不可)】
- 元データバッファ
- 切り離され、アクセス不可 (データサイズ:0)
- 対応する ~Array【型指定配列ビュー (%TypedArray%)】・DataView【データビュー】のアクセスも不可
- 関連プロパティ
関連
外部リンク
- ECMA-262 (英語)
ArrayBuffer.prototype.transfer ( [ newLength ] ) ES2024 (15) ES2023 (14) ES2022 (13) ArrayBuffer.prototype.transferToFixedLength ( [ newLength ] ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
arrayBuffer.transfer( [newLength] )
新規 ArrayBuffer【バイナリデータ配列】(サイズ変更可)
newLengthデータ転送サイズ (省略:元のデータサイズ)
※元バッファは切り離し
arrayBuffer.transferToFixedLength( [newLength] )
新規 ArrayBuffer【バイナリデータ配列】(サイズ変更不可)
newLengthデータ転送サイズ (省略:元のデータサイズ)
※元バッファは切り離し
例:transfer【データ転送 (サイズ変更可)】
const BYTES_ELM = Int16Array.BYTES_PER_ELEMENT;
console.log(BYTES_ELM);
// 出力:2
const buff = new ArrayBuffer(BYTES_ELM * 8, { maxByteLength: BYTES_ELM * 16 });
const view = new Int16Array(buff);
for (let i = 0; i < 8 ; i++)
view[i] = i + 1;
console.log(view);
// 出力:Int16Array(8) [1, 2, 3, 4, 5, 6, 7, 8, (以下省略)]
console.log(buff.detached);
// 出力:false
console.log(buff.resizable);
// 出力:true
console.log(buff.byteLength);
// 出力:16
console.log(buff.maxByteLength);
// 出力:32
// 同じサイズ分データ転送
const buff2 = buff.transfer();
console.log(buff.detached);
// 出力:true
console.log(buff2.resizable);
// 出力:true
console.log(buff2.byteLength);
// 出力:16
console.log(buff2.maxByteLength);
// 出力:32
const view2 = new Int16Array(buff2);
console.log(view2);
// 出力:Int16Array(8) [1, 2, 3, 4, 5, 6, 7, 8, (以下省略)]
// 大きいサイズ分データ転送
const buff3 = buff2.transfer(BYTES_ELM * 12);
console.log(buff2.detached);
// 出力:true
console.log(buff3.resizable);
// 出力:true
console.log(buff3.byteLength);
// 出力:24
console.log(buff3.maxByteLength);
// 出力:32
const view3 = new Int16Array(buff3);
console.log(view3);
// 出力:Int16Array(12) [1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, (以下省略)]
// 小さいサイズ分データ転送
const buff4 = buff3.transfer(BYTES_ELM * 4);
console.log(buff3.detached);
// 出力:true
console.log(buff4.resizable);
// 出力:true
console.log(buff4.byteLength);
// 出力:8
console.log(buff4.maxByteLength);
// 出力:32
const view4 = new Int16Array(buff4);
console.log(view4);
// 出力:Int16Array(4) [1, 2, 3, 4, (以下省略)]
// 切り離し済バッファ使用
// const buff5 = buff.transfer();
// 例外:TypeError
// const buff6 = buff2.transfer();
// 例外:TypeError
// const buff7 = buff3.transfer();
// 例外:TypeError
例:transferToFixedLength【データ転送 (サイズ変更不可)】
const BYTES_ELM = Int16Array.BYTES_PER_ELEMENT;
console.log(BYTES_ELM);
// 出力:2
const buff = new ArrayBuffer(BYTES_ELM * 8, { maxByteLength: BYTES_ELM * 16 });
const view = new Int16Array(buff);
for (let i = 0; i < 8 ; i++)
view[i] = i + 1;
console.log(view);
// 出力:Int16Array(8) [1, 2, 3, 4, 5, 6, 7, 8, (以下省略)]
console.log(buff.detached);
// 出力:false
console.log(buff.resizable);
// 出力:true
console.log(buff.byteLength);
// 出力:16
console.log(buff.maxByteLength);
// 出力:32
// 同じサイズ分データ転送
const buff2 = buff.transferToFixedLength();
console.log(buff.detached);
// 出力:true
console.log(buff2.resizable);
// 出力:false
console.log(buff2.byteLength);
// 出力:16
console.log(buff2.maxByteLength);
// 出力:16
const view2 = new Int16Array(buff2);
console.log(view2);
// 出力:Int16Array(8) [1, 2, 3, 4, 5, 6, 7, 8, (以下省略)]
// 大きいサイズ分データ転送
const buff3 = buff2.transferToFixedLength(BYTES_ELM * 12);
console.log(buff2.detached);
// 出力:true
console.log(buff3.resizable);
// 出力:false
console.log(buff3.byteLength);
// 出力:24
console.log(buff3.maxByteLength);
// 出力:24
const view3 = new Int16Array(buff3);
console.log(view3);
// 出力:Int16Array(12) [1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, (以下省略)]
// 小さいサイズ分データ転送
const buff4 = buff3.transferToFixedLength(BYTES_ELM * 4);
console.log(buff3.detached);
// 出力:true
console.log(buff4.resizable);
// 出力:false
console.log(buff4.byteLength);
// 出力:8
console.log(buff4.maxByteLength);
// 出力:8
const view4 = new Int16Array(buff4);
console.log(view4);
// 出力:Int16Array(4) [1, 2, 3, 4, (以下省略)]
// 切り離し済バッファ使用
// const buff5 = buff.transferToFixedLength();
// 例外:TypeError
// const buff6 = buff2.transferToFixedLength();
// 例外:TypeError
// const buff7 = buff3.transferToFixedLength();
// 例外:TypeError