From b26dc5aabf96b32c08426aa3e4bd79b77f4f5766 Mon Sep 17 00:00:00 2001 From: ComputerGuy <63362464+Ocean-OS@users.noreply.github.com> Date: Mon, 29 Sep 2025 21:23:56 -0700 Subject: [PATCH] perf: shrink `uneval` output with null-proto objects --- .changeset/happy-hoops-thank.md | 5 +++++ src/uneval.js | 13 +++++++------ test/test.js | 2 +- 3 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 .changeset/happy-hoops-thank.md 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);