Created
August 16, 2017 13:33
-
-
Save xenohunter/2393af3285ba9311101a6b75ef78691c to your computer and use it in GitHub Desktop.
Concatenate several TypedArray objects of one type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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