if I have an object which is in "slow mode" or dictionary mode, when I use Object.assign will the result be in fast mode?
example:
const foo = {'first': 1, 'second': 2}; // fast mode
delete foo.first; // puts foo in slow mode
const bar = Object.assign({}, foo);// will object bar be in slow mode?
(V8 developer here.) Short answer: In this example, bar
has "fast" properties.
You can verify this for yourself by running d8 (or node) with the --allow-natives-syntax
flag, and then using the special function %HasFastProperties(bar)
.
That said, this is an internal implementation detail that may well change over time. It should not be something that your code should have reason to care about.
Specifically, I don't know if that's why you're asking, so I'd just like to say preemptively that I would strongly recommend against building any custom "force fast mode" helpers that copy objects -- in all likelihood, that will burn more time and memory than it saves, and might suddenly stop doing what you think it does without you even knowing.
The bigger picture here is that "fast properties" and "slow properties" are misnomers. Better terms would be "fast-to-read properties" and "fast-to-modify properties": the reason that a delete
statement (usually) transitions an object to dictionary mode is because adding and removing properties is much faster when the object is in dictionary mode than when it is in (badly named) "fast-property mode". In most cases, V8's heuristics are doing a pretty good job of choosing the right internal mode for your objects, and you can generally focus on writing clean code rather than worrying about engine internals.
(If it were as simple as having a "good case" and a "bad case", then V8 would be silly for having a "bad case" at all, and you'd wish for a flag --always-good-case
(or just --dont-be-silly
), right? ;-) Reality is that it's a tradeoff: "good for one thing" vs "good for another thing".)