[英]How to copy TypedArray into another TypedArray?
C# has a high performance array copying function to copy arrays in place : C# 具有高性能的数组复制功能,可以就地复制数组:
Array.Copy(source, destination, length)
It's faster than doing it manually ie.:它比手动操作更快,即:
for (var i = 0; i < length; i++)
destination[i] = source[i];
I am looking for an equivalent high performance copy function to copy arrays in place , for Int32Array
and Float32Array
in JavaScript and can find no such function:我正在寻找一个等效的高性能复制函数来复制数组,用于 JavaScript 中的
Int32Array
和Float32Array
并且找不到这样的函数:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
The closest is copyWithin
which only does a copy internally within an array.最接近的是
copyWithin
,它只在数组内部进行复制。
Is there a built in high performance copy function for TypedArray
s in place ? TypedArray
是否有内置的高性能复制功能?
Plan B, is there a built in high performance clone function instead? B计划,是否有内置的高性能克隆功能? (EDIT: looks like
slice()
is the answer to that) (编辑:看起来像
slice()
是答案)
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:您正在寻找
.set
,它允许您使用输入数组(或 TypedArray)设置数组的值,可选择从目标数组的某个偏移量开始:
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
:如果你想创建一个新的 TypedArray,你可以简单地使用
.slice
:
source.slice();
clone to an exist typedarray:克隆到现有的类型数组:
destination.set(source);
destination.set(source, offset);
clone to a new typedarray example: (It's fastest!)克隆到一个新的 typedarray 示例:(这是最快的!)
var source = new Uint8Array([1,2,3]);
var cloned = new Uint8Array(source);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.