Proposal: `maxDepth` on objects

Tab Atkins Jr. jackalmage at gmail.com
Mon Oct 22 10:03:31 UTC 2018


On Mon, Oct 22, 2018 at 2:42 AM Rob Ede <robjtede at icloud.com> wrote:
> Calculating this on plain objects could be a O(1) operation:
>
> Empty objects are initialized with maxDepth 0 and are set to 1 when a primitive property is added.
> If an object property is added the maxDepth is set to 1 + maxDepth(newObjectProperty)`instead of it being calculated Ïevery time .maxDepth is accessed (much like how Array#length works).

That's not enough. Any time you add/change/remove a property that
would alter the depth of an object, you also need to update the depth
of every *parent* object containing it.

This isn't "actually" O(1) - it's O(1) on *access*, but only because
it's amortized the cost over every mutation instead. We don't
generally consider that trade-off worthwhile, particularly for things
like this that would have fairly specialized/limited use-cases in the
first place.

(Plus, it still doesn't answer what the depth is of an object with a
cyclic reference.)

~TJ


More information about the es-discuss mailing list