0

Can destructuring arguments cause inefficiencies if executed thousands of times?

I'm wondering if destructuring my arguments in a react application is efficient. There are many loops over many objects in many layers. How does destructuring work with javascripts pass by reference?

To test this one wrote some coffee. Why is the original updated before the test function is run?

  original =
    one:
      one: 1
    two: 2

  test = ({one, two}) ->
    one.two = two
    {one,two}

  console.log original # original.one has already beed updated!! see mu's comment. 

  destructured = test original

  console.log destructured is original, original.one is destructured.one
  # false true



{
  "one": {
    "one": 1,
    "two": 2
  },
  "two": 2
}

Destructuring appears to pass by reference. Is this efficient if executed in many many times every time react re renders?

Edit: The unexpected result is well answered by mu in the comments. The question on destructuring being a good practice is the question.

heorling
  • 339
  • 6
  • 17
  • 1
    Executive summary: `console.log` throws a live reference into the console, not the copy/snapshot you're expecting. If you manually clone `original` (`console.log _.clone(original)` or whatever cloner you have on hand) or stringify it (`console.log JSON.stringify`) then you'll see what you're expecting. – mu is too short Jul 12 '17 at 16:33
  • Still unsure if destructuing slows the rerender. I had to roll back my work and the slowdown is gone for whatever reason. – heorling Jul 13 '17 at 15:01
  • Destructing is just a couple extra assignments (see [the JavaScript version](http://coffeescript.org/#try:f%20%3D%20(%7Ba%2C%20b%7D)%20-%3E%0A%20%20a.b%20%3D%20b%0A%20%20%7Ba%2C%20b%7D%0A) for example), shouldn't be that heavy. – mu is too short Jul 13 '17 at 17:20

0 Answers0