Suggestion: Destructuring object initializer.

T.J. Crowder tj.crowder at
Wed Feb 14 09:07:09 UTC 2018

On Wed, Feb 14, 2018 at 6:25 AM, Bob Myers <rtm at> wrote:
> Actually, I did understand what he said. I didn't respond because
> it was obviously wrong.

It was obviously right. In [the message that he replied to][1] you said:

> I had thought that `{...a, ...b}` was 100% identical in every way
> to `Object.assign({}, a, b)`.

You didn't say "Identical in effect if you disregard potential performance
optimizations which the engines I checked don't seem to do based on a quick
test I did." You said "100% identical in every way." To then throw
"obviously wrong" at the man when he points out how they aren't "100%
identical in every way" is -- to put it mildly -- not polite discourse.

There are at least two differences:

1. As Alexander Jones quite correctly pointed out, `Object.assign({}, a,
b)` requires looking up `Object` in the scope chain and then requires
looking up `assign` on the object it finds. Engines are **required** to do
that or behave as though they did. They can optimize the heck out of it (as
a non-engine-implementer I can easily think of ways to optimize for the
overwhelmingly-common case where those are their default values), but
that's a difference. If you shadow or reassign `Object`, or (yikes) modify
or delete `Object.assign`, spread still works; `Object.assign` doesn't.

2. `Object.assign({}, a, b)` necessarily involves a function call; `{...a,
...b}` doesn't. Again, engines are **required** to perform a function call,
or behave such that they did. And again, in my naïveté I can think of a way
to optimize for the overwhelmingly-common-case there, but that's
optimization. It's another difference.

Those differences may or may not translate into *performance* differences
once spread support matures (last I checked, a couple of months ago, I was
surprised to find the fairly-new spread was markedly slower than
`Object.assign`, but presumably that will change as the new stuff beds in).
But they *are* differences.

So no, `Object.assign({}, a, b)` and `{...a, ...b}` are not 100% identical
in every way.

-- T.J. Crowder

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list