How to allocate 1MB in TypeScript

3 Answers

0 votes
// Allocates 1MB = 1,048,576 bytes
const buffer: Uint8Array<ArrayBuffer> = new Uint8Array(1024 * 1024); 
 
console.log(buffer.length); 
 
 
/*
run:
 
1048576 
 
*/

 



answered May 19, 2025 by avibootz
0 votes
// Allocates 1MB = 1,048,576 bytes
const array: number[] = new Array(1024 * 1024).fill(0); 

console.log(array.length);
 
 
/*
run:
 
1048576 
 
*/

 



answered May 19, 2025 by avibootz
0 votes
// Allocates 1MB = 1,048,576 bytes
const buffer: Buffer = Buffer.alloc(1024 * 1024); 

console.log(buffer.length);

 
 
/*
run:
 
1048576
 
*/

 



answered May 19, 2025 by avibootz

Related questions

3 answers 252 views
2 answers 211 views
3 answers 220 views
1 answer 156 views
156 views asked May 20, 2025 by avibootz
3 answers 238 views
3 answers 251 views
251 views asked May 20, 2025 by avibootz
1 answer 131 views
131 views asked May 20, 2025 by avibootz
...