Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

 useEffect(() => {
    xxx
  }, [Object, string])
  1. 如上面代码,useEffect通过监听Object(对象参数)和string(字符串参数)的改变,来执行副作用;
  2. 比较一个复杂的对象是否相等,和比较一个字符串是否相等,字符串是否性能更优?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
3.6k views
Welcome To Ask or Share your Answers For Others

1 Answer

  1. 每次执行渲染函数时就逐个比较依赖数组的各项和上次缓存的数据是否相等,并且采用的同值相等算法
  2. 对象类型数比较的是对象的引用,应该比比较字符串性能要好些,但是还不不要在意这一丢丢差异。
var a = 'hellowroldhellowroldhellowroldhellowroldhellowrold'
var b = a;

console.time('string');
Object.is(a, b);
console.timeEnd('string'); //  0.004150390625ms

a = {}
b = a;
console.time('object');
Object.is(a, b);
console.timeEnd('object'); // 0.001953125ms

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...