New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce bytes #401
Reduce bytes #401
Conversation
No, if I interprete it correctly, compared to the latest minified release, 2.2.0, we'll get a decrease of 6 bytes when doing a new release with these changes? @FagnerMartinsBrack |
I have just one point that's probably a matter of style, also probably be subjective and I have no evidence to support it: I've seen people having trouble to understand a codebase with lots of ternary statements. An explicit Not strong about it, though. It achieved a good result in reducing 5 bytes :) |
We're using said operator in other places as well, those weren't replaced with explicit if statements either. Thus, imo, the code is even more consistent now. I heard the argument about ternary vs if before and won't deny it, but readability in this case much depends on how huge the operands get and/or whether you start nesting ternary operators. Consistency is equally important to me. |
I'm not merging yet, because I wanted to take another look, maybe I find another place to take away some bytes. |
Agreed |
We could do if (typeof attributes.expires === 'number') {
attributes.expires = new Date(new Date().getTime() + attributes.expires * 864e+5);
} and this would result in reduction of 17 bytes altogether in this branch. I'm not too happy about creating two Date instances, but does it really matter? I would have used |
|
I don't think there's any problem with more |
I guess that would be ok because that line in particular was already obfuscated by that |
I have to agree though that
🙈 |
Honestly, as long as it has tests and doesn't break other use cases, I'm fine with it. This codebase is heavily trading off legibility for less bytes already so I don't think it makes much difference. |
Interesting, I was experimenting with making the function extend () {
var i = 0;
var result = {};
while (arguments[i]) {
for (var key in arguments[i]) {
result[key] = arguments[i][key];
}
i++;
}
return result;
} With that I'm getting a test failure
and I can't figure out why! That function works fine when experimenting in the (node) console, e.g. it produces the same result as the former one... what am I overlooking? |
Ah, it was because |
..and then the changes would turn into a This is fun. :) |
We could change collecting the result(s) to this, would increase savings to diff --git a/src/js.cookie.js b/src/js.cookie.js
index 4618358..5a90898 100644
--- a/src/js.cookie.js
+++ b/src/js.cookie.js
@@ -38,7 +38,6 @@
function init (converter) {
function api (key, value, attributes) {
- var result;
if (typeof document === 'undefined') {
return;
}
@@ -58,7 +57,7 @@
attributes.expires = attributes.expires ? attributes.expires.toUTCString() : '';
try {
- result = JSON.stringify(value);
+ var result = JSON.stringify(value);
if (/^[\{\[]/.test(result)) {
value = result;
}
@@ -90,16 +89,13 @@
// Read
- if (!key) {
- result = {};
- }
-
// To prevent the for loop in the first place assign an empty array
// in case there are no cookies at all. Also prevents odd result when
// calling "get()"
var cookies = document.cookie ? document.cookie.split('; ') : [];
var rdecode = /(%[0-9A-Z]{2})+/g;
var i = 0;
+ var jar = {};
for (; i < cookies.length; i++) {
var parts = cookies[i].split('=');
@@ -121,18 +117,15 @@
} catch (e) {}
}
+ jar[name] = cookie;
+
if (key === name) {
- result = cookie;
break;
}
-
- if (!key) {
- result[name] = cookie;
- }
} catch (e) {}
}
- return result;
+ return key ? jar[key] : jar;
}
api.set = api; |
Sounds great! You're having a lot of fun ain't you? :D |
With that we'd be at -44: diff --git a/src/js.cookie.js b/src/js.cookie.js
index 4618358..9d48419 100644
--- a/src/js.cookie.js
+++ b/src/js.cookie.js
@@ -38,7 +38,6 @@
function init (converter) {
function api (key, value, attributes) {
- var result;
if (typeof document === 'undefined') {
return;
}
@@ -58,7 +57,7 @@
attributes.expires = attributes.expires ? attributes.expires.toUTCString() : '';
try {
- result = JSON.stringify(value);
+ var result = JSON.stringify(value);
if (/^[\{\[]/.test(result)) {
value = result;
}
@@ -90,19 +89,15 @@
// Read
- if (!key) {
- result = {};
- }
-
// To prevent the for loop in the first place assign an empty array
// in case there are no cookies at all. Also prevents odd result when
// calling "get()"
var cookies = document.cookie ? document.cookie.split('; ') : [];
var rdecode = /(%[0-9A-Z]{2})+/g;
- var i = 0;
+ var jar = {};
- for (; i < cookies.length; i++) {
- var parts = cookies[i].split('=');
+ while (cookies.length) {
+ var parts = cookies.pop().split('=');
var cookie = parts.slice(1).join('=');
if (!this.json && cookie.charAt(0) === '"') {
@@ -111,8 +106,7 @@
try {
var name = parts[0].replace(rdecode, decodeURIComponent);
- cookie = converter.read ?
- converter.read(cookie, name) : converter(cookie, name) ||
+ cookie = (converter.read || converter)(cookie, name) ||
cookie.replace(rdecode, decodeURIComponent);
if (this.json) {
@@ -121,18 +115,11 @@
} catch (e) {}
}
- if (key === name) {
- result = cookie;
- break;
- }
-
- if (!key) {
- result[name] = cookie;
- }
+ jar[name] = cookie;
} catch (e) {}
}
- return result;
+ return key ? jar[key] : jar;
}
api.set = api; This removes the |
Another thing I noticed... @FagnerMartinsBrack We have attributes = extend({
path: '/'
}, api.defaults, attributes); but why is |
Ah, the test for |
I'm reluctant to remove if (key === name) {
break;
} from the loop, which would give us |
87c1060
to
7b23c3a
Compare
|
Not sure I understand you. Are you asking to merge these changes manually instead of going ahead and merging them right here? |
8da5321
to
ff0aec0
Compare
Regarding Browserstack, it's not possible testing IE 6 + 7 without buying a plan: My Parallels does in fact no longer work, the (old) version I have is not working in High Sierra. I don't have an idea how to test that. I do not even have any licensed Windows XP version any longer, otherwise I could be using Virtualbox I guess. You cannot even get these versions from https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/ |
ff0aec0
to
37bf5a4
Compare
No no. Feel free to merge from here. The commits from this PR are atomic so it should be fine to have them on As a sidenote: What I have been done so far is to close the PR from a commit on master with the message The reason I've been doing this is to be able to standardize the commit messages on However, it should be fine to merge from the Github UI with or without Squash. Up to you. |
I've signed up and contacted them, asking for an open source plan for this repository. It's possible to use it with Travis. In case we will get access, I would prepare everything to switch from Saucelabs. We would be able to automate testing browsers including IE 6 and 7 again. |
Tried converting my Paralles images to VirtualBox but all I got is a bluescreen. |
Running out of ideas, I believe I'm going to wait a little more and see whether I get to test on BrowserStack... in the meantime I'm dreaming of removing support for Internet Explorer 6 + 7. |
We can do it, it will just require a major bump to 3.0.0, that's it. What if you leave the It's better to use the milestones to help to remember what to put in the Releases changelog :D |
I still have the ambition to find out about And then, the people from BrowserStack have answered already btw:
^ If you think it's worth pursuing further, let me know, @FagnerMartinsBrack! |
Oh, ok, it seems you can just download Windows 7 from https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/, and in Windows 7 itself there's a possibility to run Windows XP, maybe that's the reason why there were no XP images. |
I think it is if you're up to it :D. Do they have a fix for this? Can they support IE6+? |
I hope, can‘t tell yet. I also remember we had problems running the full test suite in Saucelabs for PRs from outside the original repo, is that still the case?
Yes, they do! (But not in the trial, only for paid plans and, as far as I understood open source plans, hence the idea to switch.) |
Unless we choose to keep the access key in plain text, which we don’t, nope! |
37bf5a4
to
d36f69f
Compare
At the moment the test suite doesn't even run in IE6, because of Line 38 in f9f3f0e
|
But once that's fixed, I conclude this branch works fine in IE 6 with all the changes. |
This way value + key appear together, visually.
Collecting all cookies in a jar, and when looking for a specific one breaking out of the loop. By this we're also sharing one variable less (`result`) for both the write and read functionality, which will make it easier to modularize the library in the future.
Falsey is sufficient.
d36f69f
to
a7ab62c
Compare
=> 6e42d24 |
Browserstack is amazing. I'm working on moving our testing in CI to it next. |
The goal is to reduce bytes without compromising on readability.