Skip to content

Commit 561a892

Browse files
author
Guillaume Chau
committed
feat(perf): small encode gains
1 parent 57ec2ab commit 561a892

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/transfer.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
const MAX_SERIALIZED_SIZE = 512 * 1024 // 1MB
22

33
function encode (data, replacer, list, seen) {
4-
var stored, key, value, i, l
5-
var seenIndex = seen.get(data)
4+
let stored, key, value, i, l
5+
const seenIndex = seen.get(data)
66
if (seenIndex != null) {
77
return seenIndex
88
}
9-
var index = list.length
10-
var proto = Object.prototype.toString.call(data)
9+
const index = list.length
10+
const proto = Object.prototype.toString.call(data)
1111
if (proto === '[object Object]') {
1212
stored = {}
1313
seen.set(data, index)
1414
list.push(stored)
15-
var keys = Object.keys(data)
15+
const keys = Object.keys(data)
1616
for (i = 0, l = keys.length; i < l; i++) {
1717
key = keys[i]
1818
value = data[key]
19-
if (replacer) value = replacer.call(data, key, value)
19+
if (replacer) value = replacer(key, value)
2020
stored[key] = encode(value, replacer, list, seen)
2121
}
2222
} else if (proto === '[object Array]') {
@@ -25,7 +25,7 @@ function encode (data, replacer, list, seen) {
2525
list.push(stored)
2626
for (i = 0, l = data.length; i < l; i++) {
2727
value = data[i]
28-
if (replacer) value = replacer.call(data, i, value)
28+
if (replacer) value = replacer(i, value)
2929
stored[i] = encode(value, replacer, list, seen)
3030
}
3131
} else {
@@ -35,13 +35,13 @@ function encode (data, replacer, list, seen) {
3535
}
3636

3737
function decode (list, reviver) {
38-
var i = list.length
39-
var j, k, data, key, value, proto
38+
let i = list.length
39+
let j, k, data, key, value, proto
4040
while (i--) {
4141
data = list[i]
4242
proto = Object.prototype.toString.call(data)
4343
if (proto === '[object Object]') {
44-
var keys = Object.keys(data)
44+
const keys = Object.keys(data)
4545
for (j = 0, k = keys.length; j < k; j++) {
4646
key = keys[j]
4747
value = list[data[key]]

src/util.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ export function stringify (data) {
117117
return CircularJSON.stringify(data, replacer)
118118
}
119119

120-
function replacer (key) {
121-
const val = this[key]
120+
function replacer (key, val) {
122121
const type = typeof val
123122
if (Array.isArray(val)) {
124123
return val

0 commit comments

Comments
 (0)