Proposal: add an option to omit prototype of objects created by JSON.parse()

段垚 duanyao at ustc.edu
Wed Sep 28 06:36:12 UTC 2016


It is usually a bad practice to let a map-like 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




More information about the es-discuss mailing list