zloirock/core-js · error · RangeError
Wrong length
Error message
Wrong length
What it means
When constructing a typed array from an existing ArrayBuffer with no explicit length, core-js validates the buffer against the element size (BYTES). If the buffer's byte length is not a multiple of the element size, or the byteOffset/derived length would be negative or out of bounds, it throws this RangeError. This mirrors the spec's ArrayBuffer-allocated-bytes validation for `new Float32Array(buffer)` style calls.
Source
Thrown at packages/core-js/internals/typed-array-constructor.js:160
});
};
if (!NATIVE_ARRAY_BUFFER_VIEWS) {
TypedArrayConstructor = wrapper(function (that, data, offset, $length) {
anInstance(that, TypedArrayConstructorPrototype);
var index = 0;
var byteOffset = 0;
var buffer, byteLength, length;
if (!isObject(data)) {
length = toIndex(data);
byteLength = length * BYTES;
buffer = new ArrayBuffer(byteLength);
} else if (isArrayBuffer(data)) {
buffer = data;
byteOffset = toOffset(offset, BYTES);
var $len = data.byteLength;
if ($length === undefined) {
if ($len % BYTES) throw new RangeError(WRONG_LENGTH);
byteLength = $len - byteOffset;
if (byteLength < 0) throw new RangeError(WRONG_LENGTH);
} else {
byteLength = toIndex($length) * BYTES;
if (byteLength + byteOffset > $len) throw new RangeError(WRONG_LENGTH);
}
length = byteLength / BYTES;
} else if (isTypedArray(data)) {
return arrayFromConstructorAndList(TypedArrayConstructor, data);
} else {
return call(typedArrayFrom, TypedArrayConstructor, data);
}
setInternalState(that, {
buffer: buffer,
byteOffset: byteOffset,
byteLength: byteLength,
length: length,
view: new DataView(buffer)View on GitHub (pinned to 84e45fba09)
Solutions
- Check `buffer.byteLength % elementSize === 0` before constructing; slice/trim the buffer if not.
- Pass an explicit length: `new Float32Array(buffer, byteOffset, length)` with a valid length.
- Align the buffer: allocate from the start with byteLength = n * elementSize.
- Verify the byteOffset argument is a multiple of the element size and within bounds.
Example fix
// before const buf = new ArrayBuffer(10); const f64 = new Float64Array(buf); // RangeError: Wrong length // after const buf = new ArrayBuffer(16); // 2 * 8 bytes const f64 = new Float64Array(buf);
Defensive patterns
Strategy: validation
Validate before calling
function canWrap(buffer, ElementCtor, byteOffset = 0) {
const bpe = ElementCtor.BYTES_PER_ELEMENT;
return buffer.byteLength % bpe === 0 && byteOffset % bpe === 0;
}
if (!canWrap(buf, Float64Array)) buf = buf.slice(0, buf.byteLength - (buf.byteLength % 8)); Type guard
function isAligned(buffer, bytesPerElement) { return buffer instanceof ArrayBuffer && buffer.byteLength % bytesPerElement === 0; } Try / catch
try {
view = new Float64Array(buf);
} catch (e) {
if (e instanceof RangeError && e.message === 'Wrong length') {
view = new Float64Array(buf.slice(0, buf.byteLength - (buf.byteLength % 8)));
} else throw e;
} Prevention
- Ensure buffer.byteLength is a multiple of the element size before wrapping.
- Keep byteOffset aligned to the element size.
- When parsing binary formats, trim trailing padding bytes before constructing views.
When it happens
Trigger: `new Float64Array(someArrayBuffer)` where the buffer's byteLength is not divisible by 8; similarly new Uint32Array(buf) with a buffer length not divisible by 4, or where the given byteOffset exceeds the buffer length.
Common situations: Parsing binary file formats where headers leave a trailing byte, buffers built by manual concatenation with padding, or slicing an ArrayBuffer at a non-aligned offset before wrapping it in a typed array.
Related errors
- ArrayBuffer expected
- Target is not a typed array
- is not a typed array constructor
- Incorrect invocation
- Wrong index
AI-assisted analysis of zloirock/core-js@84e45fba09 (2026-08-30).
Data as JSON: /api/errors/75eb0668ef505f5f.
Report an issue: GitHub.