perf: change in operation to Object.keys (#518)

This commit is contained in:
Yuga Sun 2018-03-20 17:27:42 +08:00 committed by 花裤衩
parent 26b84847dd
commit 88c28f5d7e
1 changed files with 13 additions and 17 deletions

View File

@ -131,16 +131,14 @@ export function objectMerge(target, source) {
if (Array.isArray(source)) { if (Array.isArray(source)) {
return source.slice() return source.slice()
} }
for (const property in source) { Object.keys(source).forEach((property) => {
if (source.hasOwnProperty(property)) { const sourceProperty = source[property]
const sourceProperty = source[property] if (typeof sourceProperty === 'object') {
if (typeof sourceProperty === 'object') { target[property] = objectMerge(target[property], sourceProperty)
target[property] = objectMerge(target[property], sourceProperty) } else {
continue
}
target[property] = sourceProperty target[property] = sourceProperty
} }
} })
return target return target
} }
@ -253,15 +251,13 @@ export function deepClone(source) {
throw new Error('error arguments', 'shallowClone') throw new Error('error arguments', 'shallowClone')
} }
const targetObj = source.constructor === Array ? [] : {} const targetObj = source.constructor === Array ? [] : {}
for (const keys in source) { Object.keys(source).forEach((keys) => {
if (source.hasOwnProperty(keys)) { if (source[keys] && typeof source[keys] === 'object') {
if (source[keys] && typeof source[keys] === 'object') { targetObj[keys] = source[keys].constructor === Array ? [] : {}
targetObj[keys] = source[keys].constructor === Array ? [] : {} targetObj[keys] = deepClone(source[keys])
targetObj[keys] = deepClone(source[keys]) } else {
} else { targetObj[keys] = source[keys]
targetObj[keys] = source[keys]
}
} }
} })
return targetObj return targetObj
} }