[Immutable.js] Lightning Fast immutable.js equality Checks with Hash Codes

Source: Internet
Author: User

While Immutable.js offers. was () to confirm value equality between iterables it comes at the cost of referencing each key a nd value in both objects. For lightning fast equality checks, immutable.js can produce a hash code based on a iterable ' s content. If the iterables has the same content, the their hash codes would be the same. It's worth noting that this technique was unsuitable for mission critical application development since there are a chance, However slight, that's checksums like these might collide. This is outlined Here:https://en.wikipedia.org/wiki/collision_ (computer_science)

Mocha.setup (' BDD '); const expect=Chai.expect;class Todo {constructor (title= "", Items=immutable.list (), completed=false) {     This. ID = (+NewDate () + Math.floor (math.random () * 999999)). ToString (36);  This. title =title;  This. Items =items;  This. completed =completed; }  }functionGeneratetodos () {const Todos=[] _.each (_.range (5), index = {    varTodo =Newtodo (' Todo ${index} '); Todo.completed= Math.Round (Math.random ()) = = = 0; _.each (_.range (Math.floor (Math.random ()*100)), index ={Todo.items=Todo.items.push (' Item ${index} ');        });        Todos.push (TODO);    }); returnTodos;} Describe (' Lightning Fast equality checks with Hash Codes ', () ={It (' should take separate lists with the same items and see equal hash Codes ', () = {    varTodos =Generatetodos (); Let Todos1=Immutable.List.of (... todos); Let Todos2=Immutable.List.of (... todos);    Expect (TODOS1). To.not.equal (TODOS2);  Expect (Todos1.hashcode ()). To.equal (Todos2.hashcode ());  }); }); Mocha.run ();

[Immutable.js] Lightning Fast immutable.js equality Checks with Hash Codes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.