简体   繁体   English

angular.equals和_.isEqual之间有什么区别?

[英]What's the difference between angular.equals and _.isEqual?

I mean is there any difference in performance? 我的意思是性能有什么不同吗? Which one is able to make the best deep comparison? 哪一个能够做出最好的深度比较? Sometimes angular's equals function is not able to find every difference. 有时,角度等于函数无法找到所有差异。

I have also noticed, that the angular version of this function is not checking the '$$hashKey' key. 我也注意到,这个函数的角度版本没有检查'$$ hashKey'键。

They basically act the same by comparing the values itself and all inner properties. 它们基本上通过比较值本身和所有内部属性来起作用。 Performance is also close to being the same, at least this will be a difference in 100-200 ms for 10000 elements. 性能也接近于相同,至少对于10000个元素而言,这将是100-200毫秒的差异。 I have created small tests suite, warning: your browser page will freeze for few seconds when you run code snippet . 我创建了小测试套件, 警告:当您运行代码片段时您的浏览器页面将冻结几秒钟 I'm not totally sure is this correct way to measure performance so feel free to suggest better ways. 我不完全确定这是衡量表现的正确方法,所以请随意提出更好的方法。

 angular .module("comparsion", []) .controller("ComparsionCtrl", function($scope) { var testCases = 10000; console.time("underscore"); for(var i = 0; i < testCases; i++) { var obj = createRandomObj(5, true); var obj1 = createRandomObj(5, true); _.isEqual(obj, obj1); } console.timeEnd("underscore"); console.time("angular"); for(var i = 0; i < testCases; i++) { var obj = createRandomObj(5, true); var obj1 = createRandomObj(5, true); angular.equals(obj, obj1); } console.timeEnd("angular"); // Random object generator from http://stackoverflow.com/questions/2443901/random-object-generator-in-javascript function createRandomObj(fieldCount, allowNested) { var generatedObj = {}; for(var i = 0; i < fieldCount; i++) { var generatedObjField; switch(randomInt(allowNested ? 6 : 5)) { case 0: generatedObjField = randomInt(1000); break; case 1: generatedObjField = Math.random(); break; case 2: generatedObjField = Math.random() < 0.5 ? true : false; break; case 3: generatedObjField = randomString(randomInt(4) + 4); break; case 4: generatedObjField = null; break; case 5: generatedObjField = createRandomObj(fieldCount, allowNested); break; } generatedObj[randomString(8)] = generatedObjField; } return generatedObj; } // helper functions function randomInt(rightBound) { return Math.floor(Math.random() * rightBound); } function randomString(size) { var alphaChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; var generatedString = ''; for(var i = 0; i < size; i++) { generatedString += alphaChars[randomInt(alphaChars.length)]; } return generatedString; } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js"></script> <div ng-app="comparsion"> <div ng-controller="ComparsionCtrl"></div> </div> 

angular.equals:this is angular equals comparision. angular.equals:这是角度等于比较。

_.isEqual:this is underscore equal functionality.you need to import underscore js before using this. _.isEqual:这是下划线相同的功能。在使用它之前,你需要导入下划线js。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 angular.equals()是否可以作为角度表达式使用? - Does angular.equals() work as an angular expressions? angular.equals()占用执行时间的50% - angular.equals() takes up 50% of execution time 如何找出两个对象与 angular.equals 不相等的原因? - How can I find out why two objects do not compare equal with angular.equals? `2 == {}`和`{} == 2`之间有什么区别? - What's difference between `2 == {}` and `{} == 2` &#39;$(this)&#39;和&#39;this&#39;有什么区别? - What's the difference between '$(this)' and 'this'? (|)和(||)之间有什么区别? - What's the difference between ( | ) and ( || )? 使用toFixed和角数过滤器有什么区别? - What's the difference between using toFixed and the angular number filter? Angular中$ document和$ window.document之间有什么区别? - What's the difference between $document and $window.document in Angular? 在 Angular 8 和 9 中提供和注入“窗口”与 Window 有什么区别? - What's the difference between providing and injecting 'Window' vs Window in Angular 8 and 9? `=` 和 `==` 操作符和 `===` 有什么区别? (单、双、三等号) - What is the difference between the `=` and `==` operators and what is `===`? (Single, double, and triple equals)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM