MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
TypedArrays & ArrayBuffers0%
Study Materials
JavaScript for AdvancedProgress 0%
1.1Higher-Order Functions & Currying1.2Function Composition & Pipelining1.3Memoization & Pure Functions1.4Execution Context & Call Stack Deep Dive1.5Scope Chain & Lexical Environment
10.1TypedArrays & ArrayBuffers10.2WeakMap & WeakSet for Memory Management10.3Object.freeze, Object.seal & Immutability10.4Symbols & Well-Known Symbols
HomeTutorialsJavaScript for AdvancedTypedArrays & ArrayBuffers
WeakMap & WeakSet for Memory Management

TypedArrays & ArrayBuffers

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study Materials

Standard JavaScript arrays are dynamic, hash-like objects capable of holding heterogeneous types. While flexible, they carry substantial memory overhead and cannot directly interface with raw binary hardware protocols, WebGL shaders, audio streams, or WebAssembly. ArrayBuffer and TypedArrays provide fast, fixed-size, low-level binary memory management.


1. ArrayBuffer: The Raw Memory Slab

An ArrayBuffer represents a fixed-length contiguous block of raw binary memory:

1// Allocate a 16-byte memory block (initialized with zeros)
2const buffer = new ArrayBuffer(16);
3 
4console.log('Buffer byte length:', buffer.byteLength); // 16
Crucial Rule: You cannot directly read or write to an ArrayBuffer directly! You must access it through a View—either a TypedArray or a DataView.

2. TypedArray Views

A TypedArray interprets the underlying buffer as an array of elements of a specific numerical type:

1 Raw ArrayBuffer (16 Bytes)
2┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
3│ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 10│ 11│ 12│ 13│ 14│ 15│
4└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
5▲ ▲ ▲ ▲
6│ Int32 (4 bytes) │ Int32 (4 bytes) │ Int32 (4 bytes) │ Int32 (4 bytes)
TypedArrayElement TypeBytes per ElementValue Range
Int8Array8-bit Signed Int1-128 to 127
Uint8Array8-bit Unsigned Int10 to 255
Uint8ClampedArray8-bit Clamped Int10 to 255 (Clamps overflow, Canvas/Image data)
Int16Array16-bit Signed Int2-32,768 to 32,767
Int32Array32-bit Signed Int4-2,147,483,648 to 2,147,483,647
Float32Array32-bit Float4IEEE 754 floating point (WebGL vectors)
Float64Array64-bit Float8Standard JS Number precision
1const buffer = new ArrayBuffer(8); // 8 bytes
2 
3// View buffer as two 32-bit integers:
4const int32View = new Int32Array(buffer);
5int32View[0] = 42;
6int32View[1] = 1000;
7 
8console.log(int32View.length); // 2 elements

3. DataView & Endianness (Byte Ordering)

When parsing binary file formats (e.g. PNG headers, MP3 tags) or network packets, different systems store multi-byte numbers in different byte orders (Big-Endian vs Little-Endian). DataView allows heterogeneous reading/writing with explicit endianness control:

1const buffer = new ArrayBuffer(4);
2const view = new DataView(buffer);
3 
4// Write 16-bit int at byte offset 0 in Little-Endian format:
5view.setUint16(0, 0x1234, true); // true = little-endian
6 
7// Read back in Big-Endian:
8console.log(view.getUint16(0, false).toString(16)); // "3412" (Bytes swapped!)

4. Subarray vs. Slice (Zero-Copy Sharing)

  • typedArray.subarray(start, end): Creates a new TypedArray view sharing the exact same memory buffer (Zero copy!).
  • typedArray.slice(start, end): Creates a deep copy with a newly allocated buffer.
1const original = new Uint8Array([10, 20, 30, 40]);
2const sub = original.subarray(1, 3); // Views [20, 30]
3 
4sub[0] = 99; // Mutates shared buffer!
5console.log(original[1]); // 99! (Shared memory)

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What is an ArrayBuffer in JavaScript?
Question 2
Can you directly write values into an ArrayBuffer without using a View?
Question 3
What makes Uint8ClampedArray unique compared to a standard Uint8Array?
Question 4
What is the primary purpose of DataView?
Question 5
How does typedArray.subarray() differ from typedArray.slice()?
Next Lesson

WeakMap & WeakSet for Memory Management

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Proxy & Decorator PatternsWeakMap & WeakSet for Memory Management

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext