diff --git a/.changeset/happy-hoops-thank.md b/.changeset/happy-hoops-thank.md new file mode 100644 index 0000000..ee8ea6d --- /dev/null +++ b/.changeset/happy-hoops-thank.md @@ -0,0 +1,5 @@ +--- +"devalue": patch +--- + +perf: shrink `uneval` output with null-proto objects diff --git a/src/uneval.js b/src/uneval.js index c6af552..c64b9a2 100644 --- a/src/uneval.js +++ b/src/uneval.js @@ -245,17 +245,18 @@ export function uneval(value, replacer) { return `${type}.from(${stringify_string(thing.toString())})`; default: - const obj = `{${Object.keys(thing) + const keys = Object.keys(thing); + const obj = keys .map((key) => `${safe_key(key)}:${stringify(thing[key])}`) - .join(',')}}`; + .join(','); const proto = Object.getPrototypeOf(thing); if (proto === null) { - return Object.keys(thing).length > 0 - ? `Object.assign(Object.create(null),${obj})` - : `Object.create(null)`; + return keys.length > 0 + ? `{${obj},__proto__:null}` + : `{__proto__:null}`; } - return obj; + return `{${obj}}`; } } diff --git a/test/test.js b/test/test.js index 18ecba0..9612856 100644 --- a/test/test.js +++ b/test/test.js @@ -516,7 +516,7 @@ const fixtures = { { name: 'Object without prototype', value: Object.create(null), - js: 'Object.create(null)', + js: '{__proto__:null}', json: '[["null"]]', validate: (value) => { assert.equal(Object.getPrototypeOf(value), null);