Skip to content

Commit e2db6c4

Browse files
committed
Optmize MurmurHash for smaller filesize
1 parent dba6f84 commit e2db6c4

File tree

1 file changed

+51
-48
lines changed

1 file changed

+51
-48
lines changed

jstorage.js

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
_pubsub_observers = {},
8080

8181
/* skip published items older than current timestamp */
82-
_pubsub_last = +new Date(),
82+
_pubsub_last = +new Date(),
8383

8484
/* Next check for TTL */
8585
_ttl_timeout,
@@ -256,7 +256,7 @@
256256
function _createPolyfillStorage(type, forceCreate){
257257
var _skipSave = false,
258258
_length = 0,
259-
i,
259+
i,
260260
storage,
261261
storage_source = {};
262262

@@ -272,7 +272,7 @@
272272
return;
273273
}
274274

275-
// only IE6/7 from this point on
275+
// only IE6/7 from this point on
276276
if(_backend != "userDataBehavior"){
277277
return;
278278
}
@@ -300,7 +300,7 @@
300300
storage[i] = storage_source[i];
301301
}
302302
}
303-
303+
304304
// Polyfill API
305305

306306
/**
@@ -310,7 +310,7 @@
310310

311311
/**
312312
* Returns the key of the nth stored value
313-
*
313+
*
314314
* @param {Number} n Index position
315315
* @return {String} Key name of the nth stored value
316316
*/
@@ -345,7 +345,7 @@
345345
* Sets or updates value for a give key
346346
*
347347
* @param {String} key Key name to be updated
348-
* @param {String} value String value to be stored
348+
* @param {String} value String value to be stored
349349
*/
350350
storage.setItem = function(key, value){
351351
if(typeof value == "undefined"){
@@ -365,7 +365,7 @@
365365
}
366366

367367
storage[key] = undefined;
368-
368+
369369
_skipSave = true;
370370
if(key in storage){
371371
storage.removeAttribute(key);
@@ -783,7 +783,7 @@
783783
if(!_storage.__jstorage_meta.PubSub){
784784
_storage.__jstorage_meta.PubSub = [];
785785
}
786-
786+
787787
_storage.__jstorage_meta.PubSub.unshift([+new Date, channel, payload]);
788788

789789
_save();
@@ -795,54 +795,57 @@
795795
* JS Implementation of MurmurHash2
796796
*
797797
* SOURCE: https://github.com/garycourt/murmurhash-js (MIT licensed)
798-
*
798+
*
799799
* @author <a href="mailto:gary.court@gmail.com">Gary Court</a>
800800
* @see http://github.com/garycourt/murmurhash-js
801801
* @author <a href="mailto:aappleby@gmail.com">Austin Appleby</a>
802802
* @see http://sites.google.com/site/murmurhash/
803-
*
803+
*
804804
* @param {string} str ASCII only
805805
* @param {number} seed Positive integer only
806806
* @return {number} 32-bit positive integer hash
807807
*/
808808

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));
838-
}
839-
840-
h ^= h >>> 13;
841-
h = (((h & 0xffff) * 0x5bd1e995) + ((((h >>> 16) * 0x5bd1e995) & 0xffff) << 16));
842-
h ^= h >>> 15;
843-
844-
return h >>> 0;
845-
}
809+
function murmurhash2_32_gc(str, seed) {
810+
var
811+
l = str.length,
812+
h = (seed || 0x9747b28c) ^ l,
813+
i = 0,
814+
k,
815+
a = 0xff,
816+
b = 0xffff,
817+
c = 0x5bd1e995;
818+
819+
while (l >= 4) {
820+
k =
821+
((str.charCodeAt(i) & a)) |
822+
((str.charCodeAt(++i) & a) << 8) |
823+
((str.charCodeAt(++i) & a) << 16) |
824+
((str.charCodeAt(++i) & a) << 24);
825+
826+
k = (((k & b) * c) + ((((k >>> 16) * c) & b) << 16));
827+
k ^= k >>> 24;
828+
k = (((k & b) * c) + ((((k >>> 16) * c) & b) << 16));
829+
830+
h = (((h & b) * c) + ((((h >>> 16) * c) & b) << 16)) ^ k;
831+
832+
l -= 4;
833+
++i;
834+
}
835+
836+
switch (l) {
837+
case 3: h ^= (str.charCodeAt(i + 2) & a) << 16;
838+
case 2: h ^= (str.charCodeAt(i + 1) & a) << 8;
839+
case 1: h ^= (str.charCodeAt(i) & a);
840+
h = (((h & b) * c) + ((((h >>> 16) * c) & b) << 16));
841+
}
842+
843+
h ^= h >>> 13;
844+
h = (((h & b) * c) + ((((h >>> 16) * c) & b) << 16));
845+
h ^= h >>> 15;
846+
847+
return h >>> 0;
848+
}
846849

847850
////////////////////////// PUBLIC INTERFACE /////////////////////////
848851

@@ -883,7 +886,7 @@
883886

884887
_storage[key] = value;
885888

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

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

0 commit comments

Comments
 (0)