Skip to content

Commit 1a47e65

Browse files
committed
Replaced CRC32 hash with Murmurhash (keeping the variable names as CRC* though)
1 parent 4e426f9 commit 1a47e65

File tree

2 files changed

+70
-67
lines changed

2 files changed

+70
-67
lines changed

jstorage.js

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
(function(){
2828
var
2929
/* jStorage version */
30-
JSTORAGE_VERSION = "0.3.0",
30+
JSTORAGE_VERSION = "0.3.1",
3131

3232
/* detect a dollar object or create one if not found */
3333
$ = window.jQuery || window.$ || (window.$ = {}),
@@ -84,37 +84,6 @@
8484
/* Next check for TTL */
8585
_ttl_timeout,
8686

87-
/* crc32 table */
88-
_crc32Table = "00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 "+
89-
"0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 "+
90-
"6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 "+
91-
"FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 "+
92-
"A2677172 3C03E4D1 4B04D447 D20D85FD A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 "+
93-
"32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 "+
94-
"56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 "+
95-
"C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 "+
96-
"E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 E6635C01 "+
97-
"6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 "+
98-
"12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE "+
99-
"A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 "+
100-
"DA60B8D0 44042D73 33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 "+
101-
"5768B525 206F85B3 B966D409 CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 "+
102-
"2EB40D81 B7BD5C3B C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF "+
103-
"04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 "+
104-
"7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 6E6B06E7 "+
105-
"FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 60B08ED5 D6D6A3E8 "+
106-
"A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA AF0A1B4C "+
107-
"36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 "+
108-
"5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 "+
109-
"C2D7FFA7 B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 "+
110-
"EB0E363F 72076785 05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D "+
111-
"7CDCEFB7 0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 "+
112-
"18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 "+
113-
"A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D 3E6E77DB AED16A4A "+
114-
"D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F 30B5FFE9 BDBDF21C CABAC28A "+
115-
"53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E C4614AB8 5D681B02 "+
116-
"2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D",
117-
11887
/**
11988
* XML encoding and decoding as XML nodes can't be JSON'ized
12089
* XML nodes are encoded and decoded if the node is the value to be saved
@@ -584,7 +553,7 @@
584553
removed.push(key);
585554
continue;
586555
}
587-
if(oldCrc32List[key] != newCrc32List[key]){
556+
if(oldCrc32List[key] != newCrc32List[key] && String(oldCrc32List[key]).substr(0,2) == "2."){
588557
updated.push(key);
589558
}
590559
}
@@ -821,25 +790,58 @@
821790
_publishChange();
822791
}
823792

793+
824794
/**
825-
* CRC32 calculation based on http://noteslog.com/post/crc32-for-javascript/
795+
* JS Implementation of MurmurHash2
826796
*
827-
* @param {String} str String to be hashed
828-
* @param {Number} [crc] Last crc value in case of streams
797+
* SOURCE: https://github.com/garycourt/murmurhash-js (MIT licensed)
798+
*
799+
* @author <a href="mailto:gary.court@gmail.com">Gary Court</a>
800+
* @see http://github.com/garycourt/murmurhash-js
801+
* @author <a href="mailto:aappleby@gmail.com">Austin Appleby</a>
802+
* @see http://sites.google.com/site/murmurhash/
803+
*
804+
* @param {string} str ASCII only
805+
* @param {number} seed Positive integer only
806+
* @return {number} 32-bit positive integer hash
829807
*/
830-
function _crc32(str, crc){
831-
crc = crc || 0;
832-
833-
var n = 0, //a number between 0 and 255
834-
x = 0; //an hex number
835-
836-
crc = crc ^ (-1);
837-
for(var i = 0, len = str.length; i < len; i++){
838-
n = (crc ^ str.charCodeAt(i)) & 0xFF;
839-
x = "0x" + _crc32Table.substr(n * 9, 8);
840-
crc = (crc >>> 8)^x;
808+
809+
function murmurhash2_32_gc(str, seed) {
810+
var
811+
l = str.length,
812+
h = seed ^ l,
813+
i = 0,
814+
k;
815+
816+
while (l >= 4) {
817+
k =
818+
((str.charCodeAt(i) & 0xff)) |
819+
((str.charCodeAt(++i) & 0xff) << 8) |
820+
((str.charCodeAt(++i) & 0xff) << 16) |
821+
((str.charCodeAt(++i) & 0xff) << 24);
822+
823+
k = (((k & 0xffff) * 0x5bd1e995) + ((((k >>> 16) * 0x5bd1e995) & 0xffff) << 16));
824+
k ^= k >>> 24;
825+
k = (((k & 0xffff) * 0x5bd1e995) + ((((k >>> 16) * 0x5bd1e995) & 0xffff) << 16));
826+
827+
h = (((h & 0xffff) * 0x5bd1e995) + ((((h >>> 16) * 0x5bd1e995) & 0xffff) << 16)) ^ k;
828+
829+
l -= 4;
830+
++i;
831+
}
832+
833+
switch (l) {
834+
case 3: h ^= (str.charCodeAt(i + 2) & 0xff) << 16;
835+
case 2: h ^= (str.charCodeAt(i + 1) & 0xff) << 8;
836+
case 1: h ^= (str.charCodeAt(i) & 0xff);
837+
h = (((h & 0xffff) * 0x5bd1e995) + ((((h >>> 16) * 0x5bd1e995) & 0xffff) << 16));
841838
}
842-
return crc^(-1);
839+
840+
h ^= h >>> 13;
841+
h = (((h & 0xffff) * 0x5bd1e995) + ((((h >>> 16) * 0x5bd1e995) & 0xffff) << 16));
842+
h ^= h >>> 15;
843+
844+
return h >>> 0;
843845
}
844846

845847
////////////////////////// PUBLIC INTERFACE /////////////////////////
@@ -881,7 +883,7 @@
881883

882884
_storage[key] = value;
883885

884-
_storage.__jstorage_meta.CRC32[key] = _crc32(JSON.stringify(value));
886+
_storage.__jstorage_meta.CRC32[key] = "2."+murmurhash2_32_gc(JSON.stringify(value), 0x9747b28c);
885887

886888
this.setTTL(key, options.TTL || 0); // also handles saving and _publishChange
887889

0 commit comments

Comments
 (0)