0

Hello everyone I'm trying to figure out the fastest way to aggregate two typed arrays into a single array.

Imagine data looks like this [Int8Array, Int8Array]

and I want to end up with a single

Int8Array

Currently i'm trying to loop through the parent array and through the subarray since the typed arrays do not support concat method I can't do my normal reduce and concat way.

  for (var i = 0, len = input.length; i < len; i++) {
     for (var n = 0, len = input[i].length; n < len; n++) {
       output.push(input[i][n]);
     }
  }

But i just feel like there has to be a more efficient way to do this.

Attempting to use the set method it appears to be faster but not applicable to more than 2.

http://jsperf.com/typed-array-aggregation

asmithdev
  • 23
  • 1
  • 5
  • is it two arrays (like you wrote) or is it a 2d array (as in your code)? – javinor Apr 27 '15 at 14:13
  • Take a look at this jsperf, attempting to set more than 2 typedarrays into a single array fails. http://jsperf.com/typed-array-aggregation – asmithdev Apr 27 '15 at 14:59

3 Answers3

1

Please refer to the following link. How can I merge TypedArrays in JavaScript?

They use the set method which does not require to use a loop

Community
  • 1
  • 1
peterpod
  • 246
  • 1
  • 3
  • 11
  • Using the set method appears to be the fastest in chrome and safari, however in firefox using 2 loops is a little faster than that. http://jsperf.com/typed-array-aggregation My only concern is the set method seems great for adding 2 into a single array, but I attempted and failed at making a loop that would properly set an output array with an input array containing more than 2 subarrays. [Int8,Int8] works great [Int8,Int8,Int8,Int8] does not – asmithdev Apr 27 '15 at 14:53
  • Managed to get it working even with multiple arrays and it appears to be the fastest method across the board thus far – asmithdev Apr 27 '15 at 15:20
0

DISREGARD FOR TYPED ARRAYS

You could concatenate the arrays like so:

var arr1 = ['1','2','3'];
var arr2 = ['a','b','c'];
var concatArr = arr1.concat(arr2);

//result: ['1','2','3','a','b','c']

From Mozilla's website: Array.prototype.concat

lucusp
  • 175
  • 1
  • 2
  • 9
  • 2
    Looking at the [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) of TypedArrays, the API doesn't provide a `concat` method like normal arrays. If the OP is using a standard array and managing the types manually then `concat` would be the answer. It's hard to tell because TypedArrays don't have a `push` method either, so I'm not sure which he is using. – jusopi Apr 27 '15 at 14:17
  • Oops, my bad. Of course, you are correct. – lucusp Apr 27 '15 at 14:19
-1

To complete this task you have to go through loop. But here you can check out some other ways.

Merge keys array and values array into an object in Javascript

merge two arrays (keys and values) into an object [duplicate]

Community
  • 1
  • 1
Sohil Desai
  • 2,940
  • 5
  • 23
  • 35