3

Consider doing this:

const { pathname } = useLocation()
const { param1, param2 } = useParams()
const { push } = useHistory()

instead of:

const location = useLocation()
const params = useParams()
const history = useHistory()

const { pathname } = location
const { param1, param2 } = params
const { push } = history

Implementation:

console.log(pathname, param1, param2)
push('/next-route')

Despite being shorter in syntax, does the object destructuring in the former case bring in all properties of an object first and then assign single property to a new variable? If that's how JS works, the case then it would mean a slightly bigger load to the component. If not, there is nothing to bother about. Right?

TylerH
  • 20,799
  • 66
  • 75
  • 101
anonym
  • 4,460
  • 12
  • 45
  • 75
  • 1
    Destructuring immediately is probably a better idea to avoid variables. The more identifiers in scope at a given line of code, the larger the cognitive overhead. Other than that, there's no real difference – CertainPerformance Jul 20 '21 at 03:05
  • 1
    "does the object destructuring in former case bring in all properties of an object first and then assign single property to a new variable?" - it seems you think that returning an `object` from a function involves copying all members of that object - that is untrue: in JavaScript, [`object` values are passed by-reference](https://stackoverflow.com/questions/13104494/does-javascript-pass-by-reference), so there's no expensive object cloning going on. Also, doing `const { pathname } = useLocation()` is the same as doing `const pathname = useLocation().pathname`. – Dai Jul 20 '21 at 03:14
  • Both methods `bring in all properties of an object first and then assign single property to a new variable`. There is no such thing as partial objects in js. An object is an object. As such both methods are exactly the same (except that the second method uses a very tiny amount of extra memory, nothing you should care about, for the variables) – slebetman Jul 20 '21 at 21:48

1 Answers1

3

Is it a good practice to do

It's not a "good" practice nor is it a "bad" practice: it's just a way of writing code that modern JavaScript allows as an alternative to more verbose and hand-written object graph handling code. The same can be said of most language features (though some things, like eval and forEach, probably should be generally avoided).

Despite being shorter in syntax, does the object destructuring in former case bring in all properties of an object first and then assign single property to a new variable?

No.

It seems you think that returning an object from a function involves copying all members of that object - that is untrue: while that is what happens when you return a struct value in C, whereas in JavaScript object values can be thought of as always living on the heap and are passed by-reference, so there's no expensive object cloning going on.

So doing this:

const { pathname } = useLocation()

Is more like this:

const pathname = useLocation().pathname;

It is not the same as this (conceptually, under-the-hood):

const pathname = Object.assign( {}, useLocation() ).pathname;
Dai
  • 141,631
  • 28
  • 261
  • 374
  • I've updated the question to avoid opinion-based close votes as I don't think the root question is actually an opinion-based one. However, that has removed the first direct quote you've responded to in your answer... just wanted to inform you in case you wanted to update your answer at all. – TylerH Jul 21 '21 at 16:39