0

The idea, not a valid code, inspired by this answer:

const source1 = {x1: -5, y1: -5, x2: 10, y2: 10};
const source2 = {x1: 15, y1: 15, x2: 20, y2: 20};

// the ['s1x1'] expression becomes the variable name and x1 as its value
const [['s1x1']: x1, ['s1y1']: y1, ['s1x1']: x2, ['s1x1']: y2] = source1;
const [['s2x1']: x1, ['s2y1']: y1, ['s2x2']: x2, ['s2y2']: y2] = source2;

// use the new variables
let result = s1x1 * s2x1;

Questions

  • Is there a way to to deconstruct object into custom named variables?
  • Does it make sense in terms of performance?
thednp
  • 4,401
  • 4
  • 33
  • 45
  • 4
    `const {x1: s1x1} = source1` does destructuring assignment into a different name. I don't see how the performance would be an issue - don't microoptimise. – VLAZ Nov 20 '21 at 10:36
  • Relevant: [Which is faster?](https://ericlippert.com/2012/12/17/performance-rant/) – VLAZ Nov 20 '21 at 10:42
  • Very well, thank you, I will get myself busy measuring performance on this. – thednp Nov 20 '21 at 10:54
  • If *assignments* happen to be the biggest performance hit in your application, so much so that you need to *optimise* them, then you deserve congratulations. But otherwise, I'd focus your efforts elsewhere. – VLAZ Nov 20 '21 at 10:57
  • It's an interesting experiment to calculate 4x4 matrix multiplication quite a bit faster than the native DOMMatrix. – thednp Nov 20 '21 at 11:06

0 Answers0