zrender
zrender copied to clipboard
fix: clone object extends null
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.
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.
Or we check if the hasOwnProperty function exists first? https://jsben.ch/GaeUY
https://www.measurethat.net/Benchmarks/Show/11481/0/objectprototypehasownproperty-vs-objhasownproperty-vs-e
The performance will also drop if do null check.

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