码迷,mamicode.com
首页 > Web开发 > 详细

[Immutable.js] Lightning Fast Immutable.js Equality Checks with Hash Codes

时间:2016-02-26 06:55:16      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

While Immutable.js offers .is() to confirm value equality between iterables it comes at the cost of referencing each key and value in both objects. For lightning fast equality checks, Immutable.js can produce a hash code based on an iterable‘s content. If two iterables have the same content, their hash codes will be the same. It‘s worth noting that this technique is unsuitable for mission critical application development since there is a chance, however slight, that 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 = (+new Date() + Math.floor(Math.random() * 999999)).toString(36);
    this.title = title;
    this.items = items;
    this.completed = completed;
  }
  
}

function generateTodos() {
  
  const todos = []
  
  _.each(_.range(5), index => {
    var todo = new Todo(`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);
      
  });
  
  return todos;
}

describe(‘Lightning Fast Equality checks with Hash Codes‘, () => {
  
  it(‘should take separate lists with the same items and see equal hash codes‘, () => {

    var todos = 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

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5218912.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!