扩频运算符会影响性能吗? [英] Does spread operator affect performance?

查看:107
本文介绍了扩频运算符会影响性能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑以下两种方法来构建对象数组:

I am considering the below two approaches for building an array of objects:

方法1(列出所有属性,即使对象之间重复):

Approach 1 (list all properties, even if duplicated among objects):

const employees = [
  {
    company: 'ABC',
    country: 'IN',
    zip: 123,
    employeeId: 123,
    employeeName: 'p'
  },
  {
    company: 'ABC',
    country: 'IN',
    zip: 123,
    employeeId: 456,
    employeeName: 'q'
  },
  {
    company: 'ABC',
    country: 'IN',
    zip: 123,
    employeeId: 789,
    employeeName: 'r'
  }
];

方法2(避免与 spread运算符重复):

Approach 2 (avoid duplication with the spread operator):

const commonParams = {
  company: 'ABC',
  country: 'IN',
  zip: 123
};

const employees = [
  {
    ...commonParams,
    employeeId: 123,
    employeeName: 'p'
  },
  {
    ...commonParams,
    employeeId: 456,
    employeeName: 'q'
  },
  {
    ...commonParams,
    employeeId: 789,
    employeeName: 'r'
  }
]

方法2更简洁,并且添加一个对所有数组元素都通用的新属性会容易得多(并且不易出错).

Approach 2 is more succint, and adding a new property that is common to all array elements would be much easier (and less prone to errors).

但是,对于大的commonParams对象,与方法1相比,方法2(使用传播算子)是否会影响性能?

However, in case of a large commonParams object, does approach 2 (using the spread operator) affect performance as compared to approach 1?

spread运算符是否会遍历employees数组中每个对象的commonParams对象的每个属性?

Would the spread operator loop through each of the properties of the commonParams object for each of the objects in the employees array?

推荐答案

是的,将引用一个对象的变量传播到另一个对象中需要解释器先查找变量所指的内容,然后查找所有可枚举的变量传播的对象的属性(和关联的值),以便插入到新对象中.确实确实需要一点处理能力.

Yes, spreading a variable which refers to an object into another object requires the interpreter to look up what the variable refers to, and then look up all the enumerable own properties (and the associated values) of the object that gets spreaded so as to insert into the new object. This does indeed take a bit of processing power.

但是,在现代计算机和现代JS引擎上,所需的处理能力几乎为零;每秒可以处理数百万条指令有什么关系?几个键值对都没什么好担心的.

But, on modern computers, and on modern JS engines, the processing power required is next to nothing; what does it matter, when millions of instructions can be processed each second? A handful of key-value pairs is nothing to worry about.

除非您确定要散布具有个键值对的对象,并且实际上会导致性能瓶颈,否则效果会更好避免过早优化的想法,而是旨在编写简洁易读的代码(可以很好地使用扩展语法来调用代码).对于较大的employees数组,第二种方法比第一种方法更具可读性.

Unless you've identified that you're spreading an object with tons of key-value pairs, and it's actually causing a performance bottleneck, it would be a better idea to avoid premature optimization and aim to write clean, readable code instead (which may well invoke using spread syntax often). For a large employees array, the second approach is more readable than the first.

(不过,您也可以考虑使用.map来使代码保持DRY-er:)

(though, you also might consider using .map, to keep the code even DRY-er:)

const employeesInitial = [
  {
    employeeId: 123,
    employeeName: 'p'
  },
  {
    employeeId: 456,
    employeeName: 'q'
  },
  {
    employeeId: 789,
    employeeName: 'r'
  }
];
const employees = employeesInitial.map((obj) => ({ ...obj, ...commonParams }));

这篇关于扩频运算符会影响性能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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