zrender icon indicating copy to clipboard operation
zrender copied to clipboard

fix: clone object extends null

Open Saber2pr opened this issue 4 years ago • 4 comments

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.

Saber2pr avatar Feb 05 '21 09:02 Saber2pr

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.

pissang avatar Feb 05 '21 09:02 pissang

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

plainheart avatar Feb 05 '21 10:02 plainheart

https://www.measurethat.net/Benchmarks/Show/11481/0/objectprototypehasownproperty-vs-objhasownproperty-vs-e

The performance will also drop if do null check.

image

pissang avatar Feb 05 '21 11:02 pissang

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

plainheart avatar Feb 06 '21 01:02 plainheart