[Immutable.js] Lightning Fast Immutable.js Equality Checks with Hash Codes
2016-02-26 02:43
555 查看
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();
相关文章推荐
- 初学Phaser.js之管中窥豹
- ExtJS4的文本框(textField)使用正则表达式进行验证(Regex)的方法
- JS实现随页面滚动显示/隐藏窗口固定位置元素
- 理解javascript中Map代替循环
- 超实用的JavaScript表单代码段
- js判断鼠标位置是否在某个div中的方法
- js去字符串前后空格的实现方法
- JavaScript 定时器 SetTimeout之定时刷新窗口和关闭窗口(代码超简单)
- JavaScript代码生成PDF文件的方法
- 原生javascript实现匀速运动动画效果
- JS判断手机系统
- 简单使用JSTL攻略
- javascript Math对象方法
- FastJSON 简介及其Map/JSON/String 互转
- JSON — JSON入门与实战详解 —— JavaScript、Java与JSON互转
- [LeetCode][JavaScript]Self Crossing
- JS下实现加号减号的数量加一减一
- EL表达式杂谈
- 进阶学习js中的执行上下文
- javascript在IE下遇到的中文乱码与缓存问题