How to copy TypedArray to another TypedArray?

C # has the function of high performance copying arrays to copy arrays:

Array.Copy(source, destination, length)

This is faster than doing it manually, for example:

for (var i = 0; i < length; i++)
    destination[i] = source[i];

I am looking for an equivalent high-performance function for copying arrays into place for Int32Array and Float32Array in Javascript and cannot find such a function:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

The closest is "copyWithin", which executes an internal copy inside the array.

Is there a built-in high-performance copy feature for TypedArrays?

Plan B, is there a built-in clone function with high performance? (EDIT: looks like slice () - answer to this question)

+4
3

.set, ( TypedArray), :

destination.set(source);
destination.set(source, offset);

, :

destination.set(source.slice(limit), offset);

TypedArray, .slice:

source.slice();
+10

typedarray:

destination.set(source);
destination.set(source, offset);

typedarray: ( !)

var source = new Uint8Array([1,2,3]);
var cloned = new Uint8Array(source);
+4

, slice(0);.

var clone = myArray.slice(0);

:

Array.prototype.clone = function() {
    return this.slice(0);
};

+1

Source: https://habr.com/ru/post/1629951/


All Articles