如何将TypedArray复制到另一个TypedArray? [英] How to copy TypedArray into another TypedArray?

查看:72
本文介绍了如何将TypedArray复制到另一个TypedArray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#具有高性能的数组复制功能,可以将数组复制到位:

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

Array.Copy(source, destination, length)

比手动完成要快,即:

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

我正在寻找一种等效的高性能复制函数来就Java中的Int32Array和Float32Array复制数组 ,但找不到这样的函数:

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:

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

最接近的是"copyWithin",它仅在数组内部进行复制.

The closest is "copyWithin" which only does an copy internally within an array.

是否存在用于TypedArrays 的内置高性能复制功能?

Is there a built in high performance copy function for TypedArrays in place?

计划B,是否有内置的高性能克隆功能?(看起来slice()就是答案)

Plan B, is there a built in high performance clone function instead? ( looks like slice() is the answer to that)

推荐答案

您正在寻找

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);

如果您想创建一个新的TypedArray,则可以简单地使用 .slice :

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

source.slice();

这篇关于如何将TypedArray复制到另一个TypedArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆