解构变量和函数体的变量之间的差异

时间:2017-01-09 03:01:15

标签: javascript ecmascript-6 default-parameters

在函数创建过程中,变量实例化的以下策略(解构与主体的部分)之间的变量之间是否存在差异:

let obj = {z: {y: 99}}
let foo = ({z: {y}, x = `${y+1}`}) => console.log(x) //prints 100
let bar = (data) => {
    let y = data.z.y;
    let x = `${y+1}`;
    console.log(x);  //also prints 100
}
foo(obj);
bar(obj);

据我所知,这两个变量都会产生两个变量,但我想知道哪种方法在速度和记忆方面都是优越的。

1 个答案:

答案 0 :(得分:1)

无法明确地总结在最短时间内完成的方法。 stacksnippets和Console.WriteLine($"Hey!! {fname} whose last name would be {lname} is currently living at {myAddr} and he is working as {Designation} with so n so company."); 调用似乎也会影响结果。



console






let obj = {
  z: {
    y: 99
  }
};
console.profile("foo");
let foo = ({z: {y},x = `${y+1}`}) => console.log() //prints 100
foo(obj);
console.profileEnd("foo");

console.profile("bar");
let bar = (data) => {
  let y = data.z.y;
  let x = `${y+1}`;
  console.log(); //also prints 100
}

bar(obj);
console.profileEnd("bar");

let res = {
  foo:null,
  bar:null
}

res.foo = new Date().getTime();
console.time("foo");
for (let i = 0; i < 100; i++) {

  foo(obj);

}
console.timeEnd("foo");
res.foo = new Date().getTime() - res.foo;

res.bar = new Date().getTime();
console.time("bar");
for (let i = 0; i < 100; i++) {

  bar(obj);

}
console.timeEnd("bar");
res.bar = new Date().getTime() - res.bar;

console.log(JSON.stringify(res, null, 2));
&#13;
&#13;
&#13;