Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/happy-hoops-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"devalue": patch
---

perf: shrink `uneval` output with null-proto objects
13 changes: 7 additions & 6 deletions src/uneval.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}}`;
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down