or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

index.md

index.mddocs/

0

# typedarray-to-buffer

1

2

Convert a typed array to a Buffer without a copy. This lightweight utility provides efficient conversion between typed arrays and Node.js Buffer objects by leveraging the underlying ArrayBuffer to avoid expensive memory copies.

3

4

## Package Information

5

6

- **Package Name**: typedarray-to-buffer

7

- **Package Type**: npm

8

- **Language**: JavaScript

9

- **Installation**: `npm install typedarray-to-buffer`

10

11

## Core Imports

12

13

```javascript

14

const toBuffer = require('typedarray-to-buffer');

15

```

16

17

For environments supporting ES modules:

18

19

```javascript

20

import toBuffer from 'typedarray-to-buffer';

21

```

22

23

## Basic Usage

24

25

```javascript

26

const toBuffer = require('typedarray-to-buffer');

27

28

// Convert a Uint8Array to Buffer without copying

29

const uint8Array = new Uint8Array([1, 2, 3]);

30

const buffer = toBuffer(uint8Array);

31

32

console.log(buffer); // <Buffer 01 02 03>

33

console.log(Buffer.isBuffer(buffer)); // true

34

console.log(buffer instanceof Uint8Array); // true (original typed array augmented)

35

36

// Works with all typed array types

37

const uint32Array = new Uint32Array([1, 2, 3]);

38

const buffer32 = toBuffer(uint32Array);

39

console.log(buffer32); // <Buffer 01 00 00 00 02 00 00 00 03 00 00 00> (little-endian)

40

41

// Handles array views correctly

42

const subArray = new Uint8Array([1, 2, 3, 4, 5]).subarray(1, 4);

43

const subBuffer = toBuffer(subArray);

44

console.log(subBuffer); // <Buffer 02 03 04>

45

```

46

47

## Capabilities

48

49

### TypedArray to Buffer Conversion

50

51

Converts typed arrays to Buffer objects without copying data when possible, falling back to standard Buffer creation for non-typed array inputs.

52

53

```javascript { .api }

54

/**

55

* Convert a typed array to a Buffer without a copy

56

* @param {any} arr - Input value (typed array or any value supported by Buffer.from)

57

* @returns {Buffer} A Buffer object created from the input

58

*/

59

function typedarrayToBuffer(arr)

60

```

61

62

**Parameters:**

63

- `arr` (any): The input value to convert. Can be:

64

- Any typed array (Uint8Array, Uint16Array, Uint32Array, Int8Array, Int16Array, Int32Array, Float32Array, Float64Array)

65

- Any ArrayBufferView

66

- Any other value supported by Buffer.from() (falls back to standard Buffer creation)

67

68

**Returns:**

69

- `Buffer`: A Buffer object created from the input

70

71

**Behavior:**

72

- **For typed arrays**: Creates a Buffer using the underlying ArrayBuffer with `Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength)`. This respects the typed array's view (byteOffset and byteLength) and avoids data copying.

73

- **For non-typed arrays**: Falls back to `Buffer.from(arr)` with standard Buffer creation behavior.

74

- **Type detection**: Uses `ArrayBuffer.isView(arr)` to determine if the input is a typed array.

75

76

**Supported Typed Array Types:**

77

- `Uint8Array`, `Uint8ClampedArray`

78

- `Uint16Array`, `Uint32Array`

79

- `Int8Array`, `Int16Array`, `Int32Array`

80

- `Float32Array`, `Float64Array`

81

- Any other ArrayBufferView implementation

82

83

**Usage Examples:**

84

85

```javascript

86

const toBuffer = require('typedarray-to-buffer');

87

88

// Basic conversion

89

const arr = new Uint8Array([1, 2, 3]);

90

const buf = toBuffer(arr);

91

console.log(buf.toString('hex')); // '010203'

92

93

// Preserves byte ordering for multi-byte types

94

const arr32 = new Uint32Array([0x12345678]);

95

const buf32 = toBuffer(arr32);

96

console.log(buf32.toString('hex')); // '78563412' (little-endian on most systems)

97

98

// Handles array views (subarray)

99

const original = new Uint8Array([10, 20, 30, 40, 50]);

100

const view = original.subarray(1, 4); // [20, 30, 40]

101

const viewBuf = toBuffer(view);

102

console.log(viewBuf.toString()); // Buffer contains [20, 30, 40]

103

104

// Non-typed array fallback

105

const regularArray = [1, 2, 3];

106

const fallbackBuf = toBuffer(regularArray);

107

console.log(fallbackBuf); // Standard Buffer.from() behavior

108

```

109

110

## Error Handling

111

112

The function relies on Node.js's `Buffer.from()` for error handling:

113

- Invalid typed array inputs are handled by the underlying ArrayBuffer operations

114

- Non-typed array inputs are processed by `Buffer.from()` which may throw for unsupported types

115

- No explicit error handling is performed by the library itself

116

117

## Platform Compatibility

118

119

- **Node.js**: Uses native `Buffer.from()` implementation

120

- **Browser**: Compatible with environments supporting typed arrays and Buffer polyfill (e.g., via browserify or webpack)

121

- **Legacy browsers**: Graceful fallback through `Buffer.from()` for environments without typed array support

122

123

## Performance Notes

124

125

- **Zero-copy optimization**: When converting typed arrays, the operation creates a Buffer view of the same underlying memory, avoiding expensive data copying

126

- **Memory efficiency**: The returned Buffer shares the same ArrayBuffer as the original typed array

127

- **View preservation**: Respects `byteOffset` and `byteLength` of typed array views, ensuring correct data boundaries