解构变量性能
Destructuring Variables Performance
写入之间是否存在性能差异(如果有)
const color = props.color;
对
const { color } = props;
此外,如果我们在参数签名中进行解构,我们是否会获得或失去任何性能?参见示例 3
我假设在这种情况下 example3 是编写函数的最佳方式?
示例功能反应组件:
const example1 = (props) => {
const color = props.color;
// I know I could also just write style={{ color: props.color }}
// but for arguments sake lets say I want to write it like this.
return <h1 style={{ color }}>Hello</h1>;
};
const example2 = (props) => {
const { color } = props;
return <h1 style={{ color }}>Hello</h1>;
};
const example3 = ({ color }) => {
return <h1 style={{ color }}>Hello</h1>;
};
不会有任何性能问题,因为您的代码将是 compiled/minify 等等。
请注意,使用 React,您的代码将被转译,这与
const color = props.color
上的结果
compiler/transpiler 不一定总是会删除解构赋值,因为从 2020 年开始,所有长青浏览器都支持原生解构。根据一些证据,至少从 2018 年开始,V8 中生成的字节码通过解构赋值比传统函数参数冗长得多:
函数参数:
function add(number1, number2){
return number1 + number2;
}
const result = add(1,5);
输出字节码:
[generating bytecode for function: add]
Parameter count 3
Frame size 0
74 E> 0x2a2a0affd2a2 @ 0 : 91 StackCheck
96 S> 0x2a2a0affd2a3 @ 1 : 1d 02 Ldar a1
111 E> 0x2a2a0affd2a5 @ 3 : 2b 03 00 Add a0, [0]
121 S> 0x2a2a0affd2a8 @ 6 : 95 Return
Constant pool (size = 0)
Handler Table (size = 16)
解构赋值:
function add({number1, number2}){
return number1 + number2;
}
const result = add({number1: 1, number2: 5});
输出字节码:
[generating bytecode for function: add]
Parameter count 2
Frame size 40
74 E> 0x2c1d63b7d312 @ 0 : 91 StackCheck
0x2c1d63b7d313 @ 1 : 1f 02 fb Mov a0, r0
0x2c1d63b7d316 @ 4 : 1d fb Ldar r0
0x2c1d63b7d318 @ 6 : 89 06 JumpIfUndefined [6] (0x2c1d63b7d31e @ 12)
0x2c1d63b7d31a @ 8 : 1d fb Ldar r0
0x2c1d63b7d31c @ 10 : 88 10 JumpIfNotNull [16] (0x2c1d63b7d32c @ 26)
0x2c1d63b7d31e @ 12 : 03 3f LdaSmi [63]
0x2c1d63b7d320 @ 14 : 1e f8 Star r3
0x2c1d63b7d322 @ 16 : 09 00 LdaConstant [0]
0x2c1d63b7d324 @ 18 : 1e f7 Star r4
0x2c1d63b7d326 @ 20 : 53 e8 00 f8 02 CallRuntime [NewTypeError], r3-r4
76 E> 0x2c1d63b7d32b @ 25 : 93 Throw
76 S> 0x2c1d63b7d32c @ 26 : 20 fb 00 02 LdaNamedProperty r0, [0], [2]
0x2c1d63b7d330 @ 30 : 1e fa Star r1
85 S> 0x2c1d63b7d332 @ 32 : 20 fb 01 04 LdaNamedProperty r0, [1], [4]
0x2c1d63b7d336 @ 36 : 1e f9 Star r2
98 S> 0x2c1d63b7d338 @ 38 : 1d f9 Ldar r2
113 E> 0x2c1d63b7d33a @ 40 : 2b fa 06 Add r1, [6]
123 S> 0x2c1d63b7d33d @ 43 : 95 Return
Constant pool (size = 2)
Handler Table (size = 16)
字节码行数从函数参数的 4 行显着增加到解构赋值的 19 行。总之,截至 2018 年,在 V8 中,解构赋值的计算效率低于传统函数参数。在内存space利用率方面答案有点复杂,可以参考here.
这可能是过早的优化,但是在计算繁重的代码中,最好考虑不使用解构赋值。
我也有同感。我想破坏会消耗更多的内存。通过引用访问对象属性或数组元素时,我们正在访问相同的内存位置。当对象或数组被解构时,如果值是原始值,则 the values are copied into a new location。因此,解构确实比通过对象属性访问消耗更多的内存。
写入之间是否存在性能差异(如果有)
const color = props.color;
对
const { color } = props;
此外,如果我们在参数签名中进行解构,我们是否会获得或失去任何性能?参见示例 3
我假设在这种情况下 example3 是编写函数的最佳方式?
示例功能反应组件:
const example1 = (props) => {
const color = props.color;
// I know I could also just write style={{ color: props.color }}
// but for arguments sake lets say I want to write it like this.
return <h1 style={{ color }}>Hello</h1>;
};
const example2 = (props) => {
const { color } = props;
return <h1 style={{ color }}>Hello</h1>;
};
const example3 = ({ color }) => {
return <h1 style={{ color }}>Hello</h1>;
};
不会有任何性能问题,因为您的代码将是 compiled/minify 等等。
请注意,使用 React,您的代码将被转译,这与
const color = props.color
compiler/transpiler 不一定总是会删除解构赋值,因为从 2020 年开始,所有长青浏览器都支持原生解构。根据一些证据,至少从 2018 年开始,V8 中生成的字节码通过解构赋值比传统函数参数冗长得多:
函数参数:
function add(number1, number2){
return number1 + number2;
}
const result = add(1,5);
输出字节码:
[generating bytecode for function: add]
Parameter count 3
Frame size 0
74 E> 0x2a2a0affd2a2 @ 0 : 91 StackCheck
96 S> 0x2a2a0affd2a3 @ 1 : 1d 02 Ldar a1
111 E> 0x2a2a0affd2a5 @ 3 : 2b 03 00 Add a0, [0]
121 S> 0x2a2a0affd2a8 @ 6 : 95 Return
Constant pool (size = 0)
Handler Table (size = 16)
解构赋值:
function add({number1, number2}){
return number1 + number2;
}
const result = add({number1: 1, number2: 5});
输出字节码:
[generating bytecode for function: add]
Parameter count 2
Frame size 40
74 E> 0x2c1d63b7d312 @ 0 : 91 StackCheck
0x2c1d63b7d313 @ 1 : 1f 02 fb Mov a0, r0
0x2c1d63b7d316 @ 4 : 1d fb Ldar r0
0x2c1d63b7d318 @ 6 : 89 06 JumpIfUndefined [6] (0x2c1d63b7d31e @ 12)
0x2c1d63b7d31a @ 8 : 1d fb Ldar r0
0x2c1d63b7d31c @ 10 : 88 10 JumpIfNotNull [16] (0x2c1d63b7d32c @ 26)
0x2c1d63b7d31e @ 12 : 03 3f LdaSmi [63]
0x2c1d63b7d320 @ 14 : 1e f8 Star r3
0x2c1d63b7d322 @ 16 : 09 00 LdaConstant [0]
0x2c1d63b7d324 @ 18 : 1e f7 Star r4
0x2c1d63b7d326 @ 20 : 53 e8 00 f8 02 CallRuntime [NewTypeError], r3-r4
76 E> 0x2c1d63b7d32b @ 25 : 93 Throw
76 S> 0x2c1d63b7d32c @ 26 : 20 fb 00 02 LdaNamedProperty r0, [0], [2]
0x2c1d63b7d330 @ 30 : 1e fa Star r1
85 S> 0x2c1d63b7d332 @ 32 : 20 fb 01 04 LdaNamedProperty r0, [1], [4]
0x2c1d63b7d336 @ 36 : 1e f9 Star r2
98 S> 0x2c1d63b7d338 @ 38 : 1d f9 Ldar r2
113 E> 0x2c1d63b7d33a @ 40 : 2b fa 06 Add r1, [6]
123 S> 0x2c1d63b7d33d @ 43 : 95 Return
Constant pool (size = 2)
Handler Table (size = 16)
字节码行数从函数参数的 4 行显着增加到解构赋值的 19 行。总之,截至 2018 年,在 V8 中,解构赋值的计算效率低于传统函数参数。在内存space利用率方面答案有点复杂,可以参考here.
这可能是过早的优化,但是在计算繁重的代码中,最好考虑不使用解构赋值。
我也有同感。我想破坏会消耗更多的内存。通过引用访问对象属性或数组元素时,我们正在访问相同的内存位置。当对象或数组被解构时,如果值是原始值,则 the values are copied into a new location。因此,解构确实比通过对象属性访问消耗更多的内存。