Add an option to omit prototype of objects created by JSON.parse()?

Jordan Harband ljharb at gmail.com
Sat Oct 1 03:19:20 UTC 2016


```
JSON.parse(str, (k, b) => {
  if (v && typeof v === 'object' && !Array.isArray(v)) {
    return Object.create(null, Object.getOwnPropertyDescriptors(v));
  }
  return v;
});
```

On Mon, Sep 19, 2016 at 1:13 AM, 段垚 <duanyao at ustc.edu> wrote:

> Hi,
>
> It is usually a bad practice to let a map object (an plain object used as
> a key-value map) have a prototype.
>
> Objects created by JSON.parse() have a prototype by default, and we can
> get rid of them by:
>
>
> JSON.parse(str, function(k, v) {
>
>     if (v && typeof v === 'object' && !Array.isArray(v)) {
>
>        v.__proto__ = null;
>
>     }
>
>     return v;
>
> });
>
>
> However, implementors warn that mutating prototype causes "performance
> hazards" [1].
>
> How about adding an option to omit prototype of objects created by
> JSON.parse()?
>
> E.g.:
>
>
> JSON.parse(str, { noPrototype: true });
>
>
> [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/The_
> performance_hazards_of__%5B%5BPrototype%5D%5D_mutation
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160930/59ec0c92/attachment.html>


More information about the es-discuss mailing list