Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

react,useEffect 如何比较新旧值?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...