Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clone object extends null #719

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

fix: clone object extends null #719

wants to merge 2 commits into from

Conversation

Saber2pr
Copy link

@Saber2pr Saber2pr commented Feb 5, 2021

echarts options的source/data有时候会使用高速JSON序列化库来生成,例如json-bigint等,生成的对象没有原型。
下面是一个会发生错误的案例:

import {
    each, clone, map, isTypedArray, setAsPrimitive, isArray, isObject
    // , HashMap , createHashMap, extend, merge,
} from 'zrender/src/core/util';

const source = []
const obj = Object.create(null)
obj.product = 'Matcha Latte'
obj.count = 82
source.push(obj)

clone(source) // Error: source.hasOwnProperty is not defined.

@pissang
Copy link
Contributor

pissang commented Feb 5, 2021

These codes are usually the hotspots in common scenarios. But this change seems will slow down the hasOwnProperty check significantly according to the benchmark
https://www.measurethat.net/Benchmarks/Show/11476/0/objectprototypehasownproperty-vs-objhasownproperty

I think it's better to a deep clone outside to have a proper prototype on the objects.

@plainheart
Copy link
Collaborator

plainheart commented Feb 5, 2021

Or we check if the hasOwnProperty function exists first? https://jsben.ch/GaeUY

check the hasOwnProperty function exists
@pissang
Copy link
Contributor

pissang commented Feb 5, 2021

@plainheart
Copy link
Collaborator

Yeap, the running result of the benchmark shows obviously any extra judgment can slow the speed down. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants