Skip to content

Commit c43ecce

Browse files
committed
fixes performance issue with Mage.Cookies methods, document.cookie call is not just a string property retrieving - single call can take from 0,03 to 10 milliseconds on various browsers/devices
1 parent 1a9a75f commit c43ecce

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

lib/web/mage/cookies.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,18 @@
9191
this.get = function (name) {
9292
var arg = name + '=',
9393
aLength = arg.length,
94-
cLength = document.cookie.length,
94+
cookie = document.cookie,
95+
cLength = cookie.length,
9596
i = 0,
9697
j = 0;
9798

9899
while (i < cLength) {
99100
j = i + aLength;
100101

101-
if (document.cookie.substring(i, j) === arg) {
102+
if (cookie.substring(i, j) === arg) {
102103
return this.getCookieVal(j);
103104
}
104-
i = document.cookie.indexOf(' ', i) + 1;
105+
i = cookie.indexOf(' ', i) + 1;
105106

106107
if (i === 0) {
107108
break;
@@ -130,13 +131,14 @@
130131
* @return {String}
131132
*/
132133
this.getCookieVal = function (offset) {
133-
var endstr = document.cookie.indexOf(';', offset);
134+
var cookie = document.cookie,
135+
endstr = cookie.indexOf(';', offset);
134136

135137
if (endstr === -1) {
136-
endstr = document.cookie.length;
138+
endstr = cookie.length;
137139
}
138140

139-
return decodeURIComponent(document.cookie.substring(offset, endstr));
141+
return decodeURIComponent(cookie.substring(offset, endstr));
140142
};
141143

142144
return this;

0 commit comments

Comments
 (0)