本文翻译自:Fastest way to duplicate an array in JavaScript - slice vs. 'for' loop

In order to duplicate an array in JavaScript: which of the following is faster to use? 为了在JavaScript中复制数组,请使用以下哪项更快?

Slice method 切片方法

var dup_array = original_array.slice();

For loop For循环

for(var i = 0, len = original_array.length; i < len; ++i)dup_array[i] = original_array[i];

I know both ways do only a shallow copy : if original_array contains references to objects, objects won't be cloned, but only the references will be copied, and therefore both arrays will have references to the same objects. 我知道这两种方法都只能进行浅表复制 :如果original_array包含对对象的引用,则不会克隆对象,但只会复制引用,因此两个数组都将引用相同的对象。 But this is not the point of this question. 但这不是这个问题的重点。

I'm asking only about speed. 我只问速度。


#1楼

参考:https://stackoom.com/question/GgzE/在JavaScript中复制数组的最快方法-切片与-for-循环


#2楼

There are at least 5 (!) ways to clone an array: 至少有5种 (!)克隆数组的方法:

  • loop
  • slice 切片
  • Array.from() Array.from()
  • concat 康卡特
  • spread operator (FASTEST) 点差算子(FASTEST)

There has been a huuuge BENCHMARKS thread , providing following information: 有一个很棒的BENCHMARKS线程 ,提供以下信息:

  • for blink browsers slice() is the fastest method, concat() is a bit slower, and while loop is 2.4x slower. 对于眨眼的浏览器, slice()是最快的方法, concat()慢一点, while loop慢2.4倍。

  • for other browsers while loop is the fastest method, since those browsers don't have internal optimizations for slice and concat . 对于其他浏览器, while loop是最快的方法,因为这些浏览器没有对sliceconcat进行内部优化。

This remains true in Jul 2016. 2016年7月仍然如此。

Below are simple scripts that you can copy-paste into your browser's console and run several times to see the picture. 以下是一些简单的脚本,您可以将其复制粘贴到浏览器的控制台中,然后运行几次以查看图片。 They output milliseconds, lower is better. 他们输出毫秒,越低越好。

while loop while循环

n = 1000*1000;
start = + new Date();
a = Array(n);
b = Array(n);
i = a.length;
while(i--) b[i] = a[i];
console.log(new Date() - start);

slice 切片

n = 1000*1000;
start = + new Date();
a = Array(n);
b = a.slice();
console.log(new Date() - start);

Please note that these methods will clone the Array object itself, array contents however are copied by reference and are not deep cloned. 请注意,这些方法将克隆Array对象本身,但是数组内容是通过引用复制的,因此不会进行深度克隆。

origAr == clonedArr //returns false
origAr[0] == clonedArr[0] //returns true

#3楼

Technically slice is the fastest way. 从技术上讲, slice 最快的方法。 However , it is even faster if you add the 0 begin index. 但是 ,如果添加0开始索引,它甚至更快。

myArray.slice(0);

is faster than 比...快

myArray.slice();

http://jsperf.com/cloning-arrays/3 http://jsperf.com/cloning-arrays/3


#4楼

There is a much cleaner solution: 有一个更清洁的解决方案:

var srcArray = [1, 2, 3];
var clonedArray = srcArray.length === 1 ? [srcArray[0]] : Array.apply(this, srcArray);

The length check is required, because the Array constructor behaves differently when it is called with exactly one argument. 长度检查是必需的,因为当仅使用一个参数调用Array构造函数时,其行为会有所不同。


#5楼

深度克隆数组或对象的最简单方法:

var dup_array = JSON.parse(JSON.stringify(original_array))

#6楼

es6方式呢?

arr2 = [...arr1];

在JavaScript中复制数组的最快方法-切片与“ for”循环相关推荐

  1. java数组复制用for_在JavaScript中复制数组的最快方法 - 切片与'for'循环

    这取决于阵列的长度 . 如果数组长度<= 1,000,000, slice 和 concat 方法大约需要相同的时间 . 但是当你给出更广泛的范围时, concat 方法获胜 . 例如,尝试以下 ...

  2. php节点对象,JavaScript_JavaScript中访问节点对象的方法有哪些如何使用,JavaScript中访问节点对象的方法 - phpStudy...

    JavaScript中访问节点对象的方法有哪些如何使用 JavaScript中访问节点对象的方法有哪些? var obj = document.getElementById('fdafda'); va ...

  3. 什么是在JavaScript中扩展错误的好方法?

    本文翻译自:What's a good way to extend Error in JavaScript? I want to throw some things in my JS code and ...

  4. java中字符串和数组如何比较_[Java教程]javascript中数组和字符串的方法比较

    [Java教程]javascript中数组和字符串的方法比较 0 2016-07-19 23:00:05 ×目录[1]可索引 [2]转换 [3]拼接[4]创建[5]位置 前面的话 字符串和数组有很多的 ...

  5. JavaScript中的 inludes 和 indexOf 方法 | 判断字符串或数组中是否存在对应的元素| 相同点与不同点 | 代码详解

    目录 JavaScript中的inludes和indexOf方法 1.数组中的includes和indexOf方法比较 1.1 函数返回值的不同 1.2 函数第二个参数--开始查找的位置 1.3 in ...

  6. html escape函数,JavaScript中escape()函数的使用方法

    导语:我们在传递参数时,为了避免服务器端出现乱码,常常会要用到编码函数,urlencode.HtmlEncode.base64_encode等.本文给大家详细讲解JavaScript中escape() ...

  7. javascript控制台_超越控制台日志3种在javascript中格式化控制台输出的方法

    javascript控制台 As JavaScript developers, we intuitively use console.log() to debug, print out variabl ...

  8. 细说JavaScript中对象的属性和方法

    最近在回家的路上读了尼古拉斯的新书<JavaScript面向对象精要>,发现自己对对象的属性和方法不是很熟悉,特别是es5新增的部分,特写此文总结一下,同时也与大家共勉. 本文分为两部分, ...

  9. 深入javascript中的exec与match方法

    视频课堂https://edu.csdn.net/course/play/7621 经典面试题,如果知晓一下题目的答案,那就没有必要往下看了. var someText="web2.0 .n ...

最新文章

  1. kibana7.x操作
  2. rust为什么显示不了国服_AWS偏爱Rust,已将Rust编译器团队负责人收入囊中
  3. P1903-[国家集训队]数颜色/维护队列【带修莫队】
  4. LeetCode 1750. 删除字符串两端相同字符后的最短长度(双指针)
  5. leetcode - 740. 删除与获得点数
  6. [原创]软件测试工具简介及下载地址(不定时更新)
  7. Open3DCGAL DTM(数字地形模型)
  8. poco库 文件服务器,poco
  9. linux系统外接硬盘挂载
  10. 广义表的表头和表尾是什么?
  11. Vuex 之二:3种拿到 state 中数据并执行 getters 中方法的过程与实例剖析
  12. 问卷星禁止粘贴解决方案
  13. Linux shell脚本中分号的作用
  14. 5个超实用的浏览器插件,让你的上网体验翻天覆地!
  15. 测试工程师应具备的素质
  16. 【Web技术】1189- 你不知道的前端音视频知识
  17. Jenkins配置Coding Webhook
  18. python3实现sm2加密和签名
  19. 高中计算机八字标语,八字高考口号霸气押韵
  20. Linux 多线程编程(三)

热门文章

  1. 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能
  2. php CURL带有验证码验证登录的例子
  3. 屏幕释放第八章 Libgdx输入处理(3)输入设备
  4. Windows Server 2003 AD域升级至Windows Server 2008 R2实战案例
  5. 使用Docker Compose部署SpringBoot应用
  6. hdu 4279Number(数论)
  7. HDU 4404 Worms(多边形和圆相交)
  8. servlet路径跳转
  9. [常微分方程]Lecture 2: 欧拉数值方法及推广
  10. MySQL如何访问Postgres