Skip to content

Commit f344780

Browse files
committed
reduce array allocation in encoding maps
1 parent 0dd5479 commit f344780

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/Encoder.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,12 @@ export class Encoder {
221221
}
222222

223223
encodeMap(object: Record<string, unknown>, depth: number) {
224-
const keys = Object.keys(object);
225-
const size = keys.length;
224+
let size = 0;
225+
for (const key in object) {
226+
if (Object.prototype.hasOwnProperty.call(object, key)) {
227+
size++;
228+
}
229+
}
226230
// map
227231
if (size < 16) {
228232
// fixmap
@@ -238,9 +242,11 @@ export class Encoder {
238242
} else {
239243
throw new Error(`Too large map object: ${size}`);
240244
}
241-
for (const key of keys) {
242-
this.encodeString(key);
243-
this.encode(object[key], depth + 1);
245+
for (const key in object) {
246+
if (Object.prototype.hasOwnProperty.call(object, key)) {
247+
this.encodeString(key);
248+
this.encode(object[key], depth + 1);
249+
}
244250
}
245251
}
246252

0 commit comments

Comments
 (0)