How to copy TypedArray into another TypedArray?

前端 未结 3 1227
无人共我
无人共我 2021-01-17 10:15

C# has a high performance array copying function to copy arrays in place:

Array.Copy(source, destination, length)

It\'s faster tha

相关标签:
3条回答
  • 2021-01-17 10:52

    You can clone an array using slice(0);.

    var clone = myArray.slice(0);
    

    And you can make it a native method:

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

    Performance link comparing to loop

    0 讨论(0)
  • 2021-01-17 11:09

    You're looking for .set which allows you to set the values of an array using an input array (or TypedArray), optionally starting at some offset on the destination array:

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

    Or, to set a limited amount of the input array:

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

    If you instead want to create a new TypedArray, you can simply use .slice:

    source.slice();
    
    0 讨论(0)
  • 2021-01-17 11:10

    clone to an exist typedarray:

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

    clone to a new typedarray example: (It's fastest!)

    var source = new Uint8Array([1,2,3]);
    var cloned = new Uint8Array(source);
    
    0 讨论(0)
提交回复
热议问题