Skip to content

Commit b617c7c

Browse files
authored
perf: shrink uneval output with null-proto objects (#112)
1 parent 2635155 commit b617c7c

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

.changeset/happy-hoops-thank.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"devalue": patch
3+
---
4+
5+
perf: shrink `uneval` output with null-proto objects

src/uneval.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,17 +245,18 @@ export function uneval(value, replacer) {
245245
return `${type}.from(${stringify_string(thing.toString())})`;
246246

247247
default:
248-
const obj = `{${Object.keys(thing)
248+
const keys = Object.keys(thing);
249+
const obj = keys
249250
.map((key) => `${safe_key(key)}:${stringify(thing[key])}`)
250-
.join(',')}}`;
251+
.join(',');
251252
const proto = Object.getPrototypeOf(thing);
252253
if (proto === null) {
253-
return Object.keys(thing).length > 0
254-
? `Object.assign(Object.create(null),${obj})`
255-
: `Object.create(null)`;
254+
return keys.length > 0
255+
? `{${obj},__proto__:null}`
256+
: `{__proto__:null}`;
256257
}
257258

258-
return obj;
259+
return `{${obj}}`;
259260
}
260261
}
261262

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ const fixtures = {
516516
{
517517
name: 'Object without prototype',
518518
value: Object.create(null),
519-
js: 'Object.create(null)',
519+
js: '{__proto__:null}',
520520
json: '[["null"]]',
521521
validate: (value) => {
522522
assert.equal(Object.getPrototypeOf(value), null);

0 commit comments

Comments
 (0)