Skip to content

Commit 57ec2ab

Browse files
author
Guillaume Chau
committed
fix(perf): stringify replacer
1 parent a9b658b commit 57ec2ab

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/util.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,15 @@ export function stringify (data) {
120120
function replacer (key) {
121121
const val = this[key]
122122
const type = typeof val
123-
if (type === 'undefined') {
123+
if (Array.isArray(val)) {
124+
return val
125+
} else if (typeof val === 'string') {
126+
if (val.length > MAX_STRING_SIZE) {
127+
return val.substr(0, MAX_STRING_SIZE) + `... (${(val.length)} total length)`
128+
} else {
129+
return val
130+
}
131+
} else if (type === 'undefined') {
124132
return UNDEFINED
125133
} else if (val === Infinity) {
126134
return INFINITY
@@ -131,14 +139,15 @@ function replacer (key) {
131139
} else if (type === 'symbol') {
132140
return `[native Symbol ${Symbol.prototype.toString.call(val)}]`
133141
} else if (val !== null && type === 'object') {
134-
if (val instanceof Map || val.toString() === '[object Map]') {
142+
const proto = Object.prototype.toString.call(val)
143+
if (proto === '[object Map]') {
135144
return encodeCache.cache(val, () => getCustomMapDetails(val))
136-
} else if (val instanceof Set || val.toString() === '[object Set]') {
145+
} else if (proto === '[object Set]') {
137146
return encodeCache.cache(val, () => getCustomSetDetails(val))
138-
} else if (val instanceof RegExp) {
147+
} else if (proto === '[object RegExp]') {
139148
// special handling of native type
140149
return `[native RegExp ${RegExp.prototype.toString.call(val)}]`
141-
} else if (val instanceof Date) {
150+
} else if (proto === '[object Date]') {
142151
return `[native Date ${Date.prototype.toString.call(val)}]`
143152
} else if (val.state && val._vm) {
144153
return encodeCache.cache(val, () => getCustomStoreDetails(val))
@@ -151,8 +160,6 @@ function replacer (key) {
151160
}
152161
} else if (Number.isNaN(val)) {
153162
return NAN
154-
} else if (typeof val === 'string' && val.length > MAX_STRING_SIZE) {
155-
return val.substr(0, MAX_STRING_SIZE) + `... (${(val.length)} total length)`
156163
}
157164
return sanitize(val)
158165
}

0 commit comments

Comments
 (0)