Which is the most efficient array concat() method or push()?

Array concat method and push method which is the highest school

Array concat() method function

1.该方法不会改变原有的数组。  参数可以是array,string,number,object。
2. 返回一个新的数组,如果有数组作为参数传入concat()方法里时,添加的是数组中的元素而不是数组本身。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var arr_A = ['red','blue','green']
        var arr_B = [1,2,3,4]
        var arr_C = arr_B.concat(arr_A,"apple",999,{
    
    name:"凡夫俗子",sex: "男"})
        console.log(arr_C)
    </script>
</body>
</html>

Print result
Insert picture description here

Array concat() method mechanism

  1、 concat()在处理过程中会创建一个空的数组,然后再去循环其参数项,然后在对其一个个的进行赋值
  2、 只要不改变原数组,那么势必都会创建一个新的数组,那么自然就会响应的比较慢

in conclusion

   在实现相同的效果的时候,push往往比concat()更加的高效

Guess you like

Origin blog.csdn.net/weixin_43131046/article/details/112991811