Skip to content

Instantly share code, notes, and snippets.

@xenohunter
Created August 16, 2017 13:33
Show Gist options
  • Save xenohunter/2393af3285ba9311101a6b75ef78691c to your computer and use it in GitHub Desktop.
Save xenohunter/2393af3285ba9311101a6b75ef78691c to your computer and use it in GitHub Desktop.
Concatenate several TypedArray objects of one type
function concatTypedArrays(...args) {
if (!args.length) {
throw new Error('Nothing to concatenate!');
}
const Constructor = args[0].constructor;
args.forEach((arr) => {
if (!(arr instanceof Constructor)) {
throw new Error('Elements are of different types!');
}
});
const count = args.length;
const sumLength = args.reduce((sum, arr) => sum + arr.length, 0);
const result = new Constructor(sumLength);
let curLength = 0;
for (let i = 0; i < count; i++) {
result.set(args[i], curLength);
curLength += args[i].length;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment