Skip to content

Conversation

@loucyx
Copy link

@loucyx loucyx commented May 23, 2019

Using an arrow function instead of a function the source is smaller, and also the minified code:

export default((a,b,c,d,e)=>{for(b=b.split?b.split("."):b,d=0;d<b.length;d++)a=a?a[b[d]]:e;return a===e?c:a});

I'll understand if there is a reason related to performance or similar to not use an arrow function, but I'm making a PR if you consider this to be a good change 😄

@loucyx
Copy link
Author

loucyx commented May 23, 2019

I also noticed that if we use Array.prototype.some, the loop runs until it finds a "falsy" value and then it stops, having less "looping", an even shorter source and minified code:

export default (obj, path, def, undef) => {
	path = path.split ? path.split(".") : path;
	path.some(key => !(obj = obj ? obj[key] : undef));
	return obj === undef ? def : obj;
};
export default((a,b,c,d)=>(b=b.split?b.split("."):b,b.some(b=>!(a=a?a[b]:d)),a===d?c:a));

Then again, maybe is a performance thing.

@RReverser
Copy link
Contributor

Using an arrow function instead of a function the source is smaller, and also the minified code:

Hmm, did you use npm run build? It should also transpile code to ES5, while in your example it stays ES6 (hence the size difference).

@loucyx
Copy link
Author

loucyx commented May 23, 2019

@RReverser maybe making a PR to microbundle so the "module" build preserves the ES6 syntax instead of turning it into ES5? Also, even using microbundle, the other option I mentioned in #32 (comment) has a smaller size:

export default function(t,n,e,r){return(n=n.split?n.split("."):n).some(function(n){return!(t=t?t[n]:r)}),t===r?e:t}

And if you have and object like { foo: "bar" } and you do dlv(obj, 'foo.bar.foobar.0.whatever') it will run twice instead of 5 times (thanks to the some method).

@developit
Copy link
Owner

This library targets IE9+, so we can't rely on .some(). Modern output is enabled by default in Microbundle 0.13 (in beta), which should automate the compression optimizations from this PR.

@loucyx
Copy link
Author

loucyx commented Jul 29, 2019

Sorry @developit, I never take IE into consideration. My bad! .... Closing then!

@loucyx loucyx closed this Jul 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants