How to allocate 1MB in JavaScript

3 Answers

0 votes
const buffer = new Uint8Array(1024 * 1024); // Allocates 1MB = 1,048,576 bytes

console.log(buffer.length); 


/*
run:

1048576

*/

 



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

console.log(array.length);


/*
run:

1048576

*/

 



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

console.log(buffer.length); 


/*
run:

1048576

*/

 



answered May 19, 2025 by avibootz

Related questions

3 answers 236 views
2 answers 202 views
3 answers 215 views
1 answer 153 views
153 views asked May 20, 2025 by avibootz
3 answers 231 views
3 answers 238 views
238 views asked May 20, 2025 by avibootz
1 answer 129 views
129 views asked May 20, 2025 by avibootz
...