admin管理员组文章数量:1398802
What is the most efficient code from below.
Code 1
const {
type,
size,
} = props;
console.log(type);
Code 2*
console.log(props.type);
I read in an article there will be performance impact when you read key value pairs deep below in an object. I know accessing one level is not a huge performance impact. But I want to know from above code examples (Code 1 and Code 2) which will be faster and efficient.
What is the most efficient code from below.
Code 1
const {
type,
size,
} = props;
console.log(type);
Code 2*
console.log(props.type);
I read in an article there will be performance impact when you read key value pairs deep below in an object. I know accessing one level is not a huge performance impact. But I want to know from above code examples (Code 1 and Code 2) which will be faster and efficient.
Share Improve this question asked Dec 27, 2017 at 9:13 RyxleRyxle 8908 silver badges21 bronze badges 6- jsperf. – Sirko Commented Dec 27, 2017 at 9:15
- Your sample code 2 will be faster, as you are not creating any extra memory to store values. – Chandre Gowda Commented Dec 27, 2017 at 9:16
- Why not just ....run a benchmark yourself? This might be of use: npmjs./package/benchmark – Kamil Solecki Commented Dec 27, 2017 at 9:17
- @Ryxle check your javascript code in jsperf. – zabusa Commented Dec 27, 2017 at 9:22
- @zabusa I created a jsperf test. Please check jsperf./performance-of-object-destructuring/1 – Ryxle Commented Dec 27, 2017 at 9:37
3 Answers
Reset to default 3If you see the transpiled code for the destructing part, you can find that a new variable is being set.
For example:
const {
type,
size,
} = props;
gets converted to
var type_1 = props.type; // dummy_name
var size_1 = props.size;
So, an extra variable is being set and relatively higher memory consumption. However, the difference in performance is very less.
In this case definitely the second option (strictly this case).
There are cases where you'll sacrifice a little bit if efficiency for some readability and that's easy to judge for most people.
see the performance difference is very small but it's there.
URL: https://jsperf./destructuring-performance
In Browser and on my local node application. destructing wins! the difference was bigger in the browser, But if the transpilers are messing with the js code , then there are chances that destructuring might have a negative impact on the "build" time.
const user = {
name: 'John',
age: 30,
email: '[email protected]',
};
console.time('Direct Key Access');
for (let i = 0; i < 1000000; i++) {
const name = user.name;
const age = user.age;
const email = user.email;
// Perform an operation with the values
const result = `${name} is ${age} years old and their email is ${email}`;
}
console.timeEnd('Direct Key Access');
console.time('Destructuring');
for (let i = 0; i < 1000000; i++) {
const { name, age, email } = user;
// Perform an operation with the values
const result = `${name} is ${age} years old and their email is ${email}`;
}
console.timeEnd('Destructuring');
本文标签: javascriptPerformance between direct key access and object destructuringStack Overflow
版权声明:本文标题:javascript - Performance between direct key access and object destructuring - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744609627a2615572.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论