Proposal: `maxDepth` on objects

Rob Ede robjtede at icloud.com
Mon Oct 22 09:41:43 UTC 2018


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).

On 22 Oct 2018, at 00:58, Siegfried Bilstein <sbilstein at gmail.com <mailto:sbilstein at gmail.com>> wrote:

> Wouldn't calculating the depth open up objects for abuse? Assuming the max depth is calculated with a BFS, it seems like you end up with a potentially expensive BFS in the standard. Something safer may be  'hasDepth(int n)' or a 'depthLessThan(int n)'.
> 
> On Sun, Oct 21, 2018 at 13:51 Jordan Harband <ljharb at gmail.com <mailto:ljharb at gmail.com>> wrote:
> What would it report on `obj` with `const a = {}; const obj = { a };`? What about with `const obj = { get a() { return Math.random() > 0.5 ? obj : {}; } };`?
> 
> This seems like something you could do yourself as a function.
> 
> On Sun, Oct 21, 2018 at 7:30 AM Isiah Meadows <isiahmeadows at gmail.com <mailto:isiahmeadows at gmail.com>> wrote:
> Are there other use cases for this? This reason doesn't really seem compelling.
> 
> (Not TC39, but I wouldn't expect this to be considered without a way stronger use case.)
> On Sun, Oct 21, 2018 at 09:45 Oliver Dunk <oliver at oliverdunk.com <mailto:oliver at oliverdunk.com>> wrote:
> I’d love to see some sort of `maxDepth` property on objects.
> 
> For `{}`, it would return `0`.
> In the case of `{keyOne: true}`, it would return `1`.
> For `{keyOne: {anotherKey: false}, keyTwo: false}`, it would return `2`.
> 
> My particular use case is validating a JSON payload sent by a user to prevent abuse. I don’t want to force a particular structure or set of keys, but I do want to make sure the level of nesting doesn’t get silly.
> 
> The code to implement this is fairly short, but looks a bit messy. It’d be really nice to have a property instead.
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
> https://mail.mozilla.org/listinfo/es-discuss <https://mail.mozilla.org/listinfo/es-discuss>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
> https://mail.mozilla.org/listinfo/es-discuss <https://mail.mozilla.org/listinfo/es-discuss>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
> https://mail.mozilla.org/listinfo/es-discuss <https://mail.mozilla.org/listinfo/es-discuss>
> -- 
> Sent from Gmail Mobile
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20181022/10317b71/attachment.html>


More information about the es-discuss mailing list