Add an option to omit prototype of objects created by JSON.parse()?
# Jordan Harband (9 years ago)
JSON.parse(str, (k, b) => {
if (v && typeof v === 'object' && !Array.isArray(v)) {
return Object.create(null, Object.getOwnPropertyDescriptors(v));
}
return v;
});
```
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># 段垚 (9 years ago)
I thought my original mail did not reach es-discuss and re-sent it.
Please refer to esdiscuss.org/topic/proposal-add-an-option-to-omit-prototype-of-objects-created-by-json-parse
It seems the overhead of creating a prototypeless copy is significant,
and Object.getOwnPropertyDescriptors(v) creates even more objects.
在 2016/10/1 11:19, Jordan Harband 写道:
I thought my original mail did not reach es-discuss and re-sent it.
Please refer to
https://esdiscuss.org/topic/proposal-add-an-option-to-omit-prototype-of-objects-created-by-json-parse
It seems the overhead of creating a prototypeless copy is significant,
and `Object.getOwnPropertyDescriptors(v)` creates even more objects.
在 2016/10/1 11:19, Jordan Harband 写道:
> ```
> 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
> <mailto: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
> <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 <mailto:es-discuss at mozilla.org>
> https://mail.mozilla.org/listinfo/es-discuss
> <https://mail.mozilla.org/listinfo/es-discuss>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20161001/2781fccc/attachment.html># Bob Myers (9 years ago)
What is the problem with the object resulting from JSON.parse having a
prototype again?
What is the problem with the object resulting from `JSON.parse` having a
prototype again?
On Sat, Oct 1, 2016 at 12:57 PM, 段垚 <duanyao at ustc.edu> wrote:
> I thought my original mail did not reach es-discuss and re-sent it.
>
> Please refer to https://esdiscuss.org/topic/proposal-add-an-option-to-
> omit-prototype-of-objects-created-by-json-parse
>
> It seems the overhead of creating a prototypeless copy is significant, and
> `Object.getOwnPropertyDescriptors(v)` creates even more objects.
>
> 在 2016/10/1 11:19, Jordan Harband 写道:
>
> ```
> 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
>>
>
>
>
> _______________________________________________
> 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/20161001/ed8ce723/attachment.html># 段垚 (9 years ago)
One of the explanations:
"The pitfalls of using objects as maps in JavaScript" www.2ality.com/2012/01/objects-as-maps.html
在 2016/10/1 21:19, Bob Myers 写道:
One of the explanations:
"The pitfalls of using objects as maps in JavaScript"
http://www.2ality.com/2012/01/objects-as-maps.html
在 2016/10/1 21:19, Bob Myers 写道:
> What is the problem with the object resulting from `JSON.parse` having
> a prototype again?
>
>
> On Sat, Oct 1, 2016 at 12:57 PM, 段垚 <duanyao at ustc.edu
> <mailto:duanyao at ustc.edu>> wrote:
>
> I thought my original mail did not reach es-discuss and re-sent it.
>
> Please refer to
> https://esdiscuss.org/topic/proposal-add-an-option-to-omit-prototype-of-objects-created-by-json-parse
> <https://esdiscuss.org/topic/proposal-add-an-option-to-omit-prototype-of-objects-created-by-json-parse>
>
>
> It seems the overhead of creating a prototypeless copy is
> significant, and `Object.getOwnPropertyDescriptors(v)` creates
> even more objects.
>
>
> 在 2016/10/1 11:19, Jordan Harband 写道:
>> ```
>> 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
>> <mailto: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
>> <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 <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>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20161002/dbcf66d4/attachment.html>
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) {
});
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] developer.mozilla.org/en-US/docs/Web/JavaScript/The_performance_hazards_of__[[Prototype]]_mutation
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