Skip to content
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

Shaving some bytes with reduce #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Shaving some bytes with reduce #8

wants to merge 1 commit into from

Conversation

robinvdvleuten
Copy link

Hi there! By using an array reduce on the key, you'll can go even leaner with 124 bytes 💪

@kevlened
Copy link

Unfortunately, after minification, using reduce is actually larger (using https://jscompress.com/ which uses UglifyJS 3 and Babili):

original (119 bytes):
export default function dlv(a,b,c,d){for(d=0,b=b.split?b.split('.'):b;a&&d<b.length;)a=a[b[d++]];return a===void 0?c:a}

with reduce (123 bytes):
export default function dlv(a,b,c){return(a=(b.split?b.split('.'):b).reduce(function(d,e){return d&&d[e]},a))===void 0?c:a}

@developit
Copy link
Owner

Thanks for the great explanation @kevlened! This also jives with my experience. Worth noting that performance is also at play here, and reduce() has fairly poor performance.

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