From 461e08c65b4e824cca6a36a4034530827fa9f2ef Mon Sep 17 00:00:00 2001 From: Alexey Novikov Date: Fri, 4 Jul 2014 10:12:26 +1200 Subject: [PATCH 1/2] Optimization for IE11 event handling. Event handler called multiple times and if storage holds significant number of records that kills the browser on each iteration when it attempts keep values in sync. This fix aimed to reduce number of dummy calls when stored value hasn't changed, and optimize its sync logic. --- ngStorage.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/ngStorage.js b/ngStorage.js index 0d71a2d..4d4d3b1 100644 --- a/ngStorage.js +++ b/ngStorage.js @@ -87,15 +87,21 @@ }); // #6: Use `$window.addEventListener` instead of `angular.element` to avoid the jQuery-specific `event.originalEvent` - 'localStorage' === storageType && $window.addEventListener && $window.addEventListener('storage', function(event) { - if ('ngStorage-' === event.key.slice(0, 10)) { - event.newValue ? $storage[event.key.slice(10)] = angular.fromJson(event.newValue) : delete $storage[event.key.slice(10)]; - - _last$storage = angular.copy($storage); - - $rootScope.$apply(); - } - }); + 'localStorage' === storageType && $window.addEventListener && $window.addEventListener('storage', function(event) { + if (!angular.equals(event.newValue, event.oldValue)) { + var pr = event.key.slice(0, 10), key = event.key.slice(10); + if ('ngStorage-' === pr) { + if (event.newValue) { + $storage[key] = angular.fromJson(event.newValue); + _last$storage[key] = angular.copy($storage[key]); + } else { + delete $storage[key]; + } + + $rootScope.$apply(); + } + } + }, false); return $storage; } From 475818dc3fb1c817e15f4178ce4151cadba373e4 Mon Sep 17 00:00:00 2001 From: Alexey Novikov Date: Mon, 21 Jul 2014 16:00:30 +1200 Subject: [PATCH 2/2] Performance optimization --- ngStorage.js | 38 ++++++++++++++++++++------------------ ngStorage.min.js | 2 +- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/ngStorage.js b/ngStorage.js index 4d4d3b1..350b6b7 100644 --- a/ngStorage.js +++ b/ngStorage.js @@ -68,11 +68,12 @@ $rootScope.$watch(function() { _debounce || (_debounce = setTimeout(function() { - _debounce = null; - if (!angular.equals($storage, _last$storage)) { angular.forEach($storage, function(v, k) { - angular.isDefined(v) && '$' !== k[0] && webStorage.setItem('ngStorage-' + k, angular.toJson(v)); + angular.isDefined(v) + && '$' !== k[0] + && !angular.equals($storage[k], _last$storage[k]) + && webStorage.setItem('ngStorage-' + k, angular.toJson(v)); delete _last$storage[k]; }); @@ -83,25 +84,26 @@ _last$storage = angular.copy($storage); } + _debounce = null; }, 100)); }); // #6: Use `$window.addEventListener` instead of `angular.element` to avoid the jQuery-specific `event.originalEvent` - 'localStorage' === storageType && $window.addEventListener && $window.addEventListener('storage', function(event) { - if (!angular.equals(event.newValue, event.oldValue)) { - var pr = event.key.slice(0, 10), key = event.key.slice(10); - if ('ngStorage-' === pr) { - if (event.newValue) { - $storage[key] = angular.fromJson(event.newValue); - _last$storage[key] = angular.copy($storage[key]); - } else { - delete $storage[key]; - } - - $rootScope.$apply(); - } - } - }, false); + 'localStorage' === storageType && $window.addEventListener && $window.addEventListener('storage', function (event) { + if (!angular.equals(event.newValue, event.oldValue)) { + var pr = event.key.slice(0, 10), key = event.key.slice(10); + if ('ngStorage-' === pr) { + if (event.newValue) { + $storage[key] = angular.fromJson(event.newValue); + _last$storage[key] = angular.copy($storage[key]); + } else { + delete $storage[key]; + } + + $rootScope.$apply(); + } + } + }, false); return $storage; } diff --git a/ngStorage.min.js b/ngStorage.min.js index f5526bb..b1257d2 100644 --- a/ngStorage.min.js +++ b/ngStorage.min.js @@ -1 +1 @@ -/*! ngStorage 0.3.0 | Copyright (c) 2013 Gias Kay Lee | MIT License */"use strict";!function(){function a(a){return["$rootScope","$window",function(b,c){for(var d,e,f,g=c[a]||(console.warn("This browser does not support Web Storage!"),{}),h={$default:function(a){for(var b in a)angular.isDefined(h[b])||(h[b]=a[b]);return h},$reset:function(a){for(var b in h)"$"===b[0]||delete h[b];return h.$default(a)}},i=0;i