zloirock/core-js · error · RangeError

Wrong length

Error message

Wrong length

What it means

TypeError-less RangeError thrown by the core-js polyfill of TypedArray.prototype.set(arrayLike, offset). The polyfill manually copies elements when the engine lacks generic typed-array set support, and before copying it checks whether source length plus the target offset exceeds the target typed array's length. If it does, the write would overflow the destination buffer, so a RangeError('Wrong length') is thrown.

Source

Thrown at packages/core-js/modules/es.typed-array.set.js:42

// https://bugs.chromium.org/p/v8/issues/detail?id=11294 and other
var TO_OBJECT_BUG = WORKS_WITH_OBJECTS_AND_GENERIC_ON_TYPED_ARRAYS && ArrayBufferViewCore.NATIVE_ARRAY_BUFFER_VIEWS && fails(function () {
  var array = new Int8Array(2);
  array.set(1);
  array.set('2', 1);
  return array[0] !== 0 || array[1] !== 2;
});

// `%TypedArray%.prototype.set` method
// https://tc39.es/ecma262/#sec-%typedarray%.prototype.set
exportTypedArrayMethod('set', function set(arrayLike /* , offset */) {
  aTypedArray(this);
  var offset = toOffset(arguments.length > 1 ? arguments[1] : undefined, 1);
  var src = toIndexedObject(arrayLike);
  if (WORKS_WITH_OBJECTS_AND_GENERIC_ON_TYPED_ARRAYS) return call($set, this, src, offset);
  var length = this.length;
  var len = lengthOfArrayLike(src);
  var index = 0;
  if (len + offset > length) throw new RangeError('Wrong length');
  while (index < len) this[offset + index] = src[index++];
}, !WORKS_WITH_OBJECTS_AND_GENERIC_ON_TYPED_ARRAYS || TO_OBJECT_BUG);

View on GitHub (pinned to 84e45fba09)

Solutions

  1. Check src.length + offset <= typedArray.length before calling set, or slice the source
  2. Increase the destination typed array size or use subarray to create a view of adequate length
  3. Use offset 0 and a correctly sized source array
  4. If the error appears only on legacy browsers, guard with a length check that works cross-engine

Example fix

// before
typedArray.set(newData, 10); // RangeError if newData.length + 10 > typedArray.length
// after
if (newData.length + 10 <= typedArray.length) {
  typedArray.set(newData, 10);
} else {
  typedArray.set(newData.subarray(0, typedArray.length - 10), 10);
}
Defensive patterns

Strategy: validation

Validate before calling

function canSet(target, src, offset = 0) {
  return Number.isInteger(offset) && offset >= 0 &&
    src.length + offset <= target.length;
}

Type guard

function isTypedArrayWithRoom(ta, srcLen, offset) {
  return ArrayBuffer.isView(ta) &&
    (ta.length ?? 0) + 0 >= srcLen + offset;
}

Try / catch

try {
  ta.set(src, offset);
} catch (e) {
  if (e instanceof RangeError && /Wrong length/.test(e.message)) {
    ta.set(src.subarray(0, ta.length - offset), offset);
  } else throw e;
}

Prevention

When it happens

Trigger: Calling typedArray.set(arrayLike, offset) via the core-js polyfill where arrayLike.length + offset > typedArray.length — e.g. new Int8Array(4).set([1,2,3,4,5]) or new Uint8Array(8).set(src, 6) with src.length 5 (6+5>8). Only thrown on engines where WORKS_WITH_OBJECTS_AND_GENERIC_ON_TYPED_ARRAYS is false (e.g. old IE); modern engines throw their own RangeError from native set.

Common situations: Copying a larger buffer chunk into a smaller view without slicing; an off-by-one or wrong offset constant; assuming set() truncates like subarray; running core-js polyfills on legacy browsers (IE11) where the native fast path is unavailable.

Related errors


AI-assisted analysis of zloirock/core-js@84e45fba09 (2026-08-30). Data as JSON: /api/errors/06f3c60ea2b1d611. Report an issue: GitHub.